PreparedStatement,using one parameter for multiple “?”

后端 未结 4 416
名媛妹妹
名媛妹妹 2021-01-24 13:08

I have a insert if not exists query as below.

BEGIN
    IF NOT EXISTS (SELECT * FROM tbl_sampleTable WHERE name = ? or subject = ?)
    BEGIN
        INSERT INTO         


        
4条回答
  •  清歌不尽
    2021-01-24 14:02

    Just in this case you might use SQL variables. It is not a general solution. And also many SQL vendor specific variants know such insert-when-not-exists constructs, not needing such archaic code.

    BEGIN
        DECLARE @MyName varchar(100);
        DECLARE @MySubject varchar(100);
        SET @MyName = ?;
        SET @MySubject = ?;
        IF NOT EXISTS (SELECT * FROM tbl_sampleTable WHERE name = @MyName OR subject = @MySubject)
        BEGIN
            INSERT INTO tbl_sampleTable(subject, name) VALUES (@MySubject, @MyName)
        END
    END
    

提交回复
热议问题