0

This is the code I have:

INSERT  INTO HCSERVREC (COMP_CD, JOBTL_CD, emp_no,from_dt,service_type)
values             ( '1',( if NOT EXISTS(
    SELECT 1
    FROM SDOPTTABLE
    WHERE OPTTABLENO = '324'
        AND OPTTABLEVAL = '00883578'
    )
BEGIN
INSERT INTO SDOPTTABLE (
    comp_cd
    ,opttableno
    ,opttableval
    ,optname
    ,dt_stamp
    )
VALUES (
    '1'
    ,'324'
    ,'00883578'
    ,'STOCKROOM ATTENDANT'
    ,getdate()
    )
END

SELECT *
FROM SDOPTTABLE
WHERE OPTTABLENO = '324'
AND OPTTABLEVAL = '00883578'),'1234567','0','123456')

These are the error messages I get:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'if'.

Msg 102, Level 15, State 1, Line 28
Incorrect syntax near ')'.

All I want to do is verify before insert data input.how to do it correctly?

FYI, when I replace the if statement with single data the sql work fine, even if I run the IF statement independently is also work just fine. just the issue when the both are combine.

Thanks.

2
  • The statements are not clear. There are two insert statement and also full block is not complete one.. Can you please give full block and tell details Commented Sep 9, 2014 at 4:39
  • 1
    YOu cannot mix an INSERT statement with an IF clause - if you need to check something (like the existence of a row), you need to check this first, and then either do your INSERT (or skip it) Commented Sep 9, 2014 at 4:56

4 Answers 4

2

Check first and then insert

IF NOT EXISTS(
    SELECT 1
    FROM SDOPTTABLE
    WHERE OPTTABLENO = '324'
        AND OPTTABLEVAL = '00883578'
    )
BEGIN
INSERT INTO SDOPTTABLE (
    comp_cd
    ,opttableno
    ,opttableval
    ,optname
    ,dt_stamp
    )
VALUES (
    '1'
    ,'324'
    ,'00883578'
    ,'STOCKROOM ATTENDANT'
    ,getdate()
    )
END
INSERT  INTO HCSERVREC (COMP_CD, JOBTL_CD, emp_no,from_dt,service_type)
SELECT '1',
      (SELECT * -- select only the required column. You cannot use * here
       FROM SDOPTTABLE
       WHERE OPTTABLENO = '324'
       AND OPTTABLEVAL = '00883578'),
       '1234567',
       '0',
       '123456'
Sign up to request clarification or add additional context in comments.

2 Comments

i get what you mean, but what if i have more than one statement need to be check before insert? how am i gonna to do that? how many times "IF NOT EXISTS...." i need to be write before i started to do insertion, can i write multiple times?? @Raj
Yes. You can have multiple checks
0

You need to do your check first, before you then either run your INSERT, or skip it. You cannot have IF statements in the middle of an INSERT:

IF NOT EXISTS(SELECT * FROM dbo.SDOPTTABLE WHERE OPTTABLENO = '324' AND OPTTABLEVAL = '00883578')
    INSERT INTO HCSERVREC (COMP_CD, JOBTL_CD, emp_no,from_dt,service_type)
    VALUES ('1', .......)

And the INSERT statement can take two forms - either you have a fixed list of values as literals ('1') or SQL variables - then you can use the INSERT INTO Table(columns) VALUES(values) approach.

Or you want to use a SELECT statement from another table to insert the values, then you need to use the INSERT INTO Table(columns) SELECT column FROM OtherTable WHERE condition style. Again: you can pick one or the other - but you cannot mix them (e.g. you cannot have a sub-SELECT in the middle of a VALUES(., ., ....) enumeration

1 Comment

hi bro, i get what you mean, but what if i have more than one statement need to be check before insert? how am i gonna to do that? how many times "IF NOT EXISTS...." i need to be write before i started to do insertion, can i write multiple times??
0

I would suggest that you do this as a single statement, using insert . . . select:

INSERT INTO HCSERVREC(COMP_CD, JOBTL_CD, emp_no,from_dt,service_type)
    SELECT '1', '324', '00883578', 'STOCKROOM ATTENDANT', getdate()
    WHERE NOT EXISTS (SELECT 1
                      FROM SDOPTTABLE
                      WHERE OPTTABLENO = '324' AND OPTTABLEVAL = '00883578'
                     );

1 Comment

let say i have more than one input need to be checking before doing insertion, what should i do?
0

That's not possible. While inserting you need to get only one value for one column.

Either as suggest first check not exist select query and then insert.

yes, you can use case or subquery to get a single value to replace if statement above.

updated

If you wish this type of situation, then you can create dynamic query for this.

--declare table1 table ( id int, value varchar(10) )
--drop table Table1
begin tran
create table table1 ( id int, value varchar(10), value1 varchar(10), value2 varchar(10))
insert into table1 values( 1,'001','A','A1')
insert into table1 values(2, '002','B','B1')
insert into table1 values( 3,'003','C','C1')

-----insert example
--always use nvarchar for dynamic query, other wise gives error
declare @sql nvarchar(max), @columnlist nvarchar(max) = 'id',  @columnlistvalue nvarchar(max) ='4'
declare @checkvalue varchar = '1',@checkvalue2 varchar = '2',@checkvalue3 varchar, 
@columnvalueFirst varchar(10) = 'this entry done by dynamic query on condition basis'

if(@checkvalue = 1)
  begin
    set @columnlist = @columnlist + ',value'
    set @columnlistvalue = @columnlistvalue + ',''004''' --or you can add @checkvalue
  end
if(@checkvalue2 = 2)
 begin
    --this is example, you can add another condition. Right now if else condition gives same output
    if( len(@columnlist) > 0)  
    begin
        set @columnlist = @columnlist + ',value1'
        set @columnlistvalue = @columnlistvalue + ',''D''' --or you can add @checkvalue2
    end
    else
    begin
        set @columnlist = @columnlist + ',value'
        set @columnlistvalue = @columnlistvalue + ',''D'''
    end
 end    
if(@checkvalue3 = 3)
 begin
    --this is example, you can add another condition. Right now if else condition gives same output
    if( len(@columnlist) > 0)  
    begin
        set @columnlist = @columnlist + ',value2'
        set @columnlistvalue = @columnlistvalue + ',''D1''' --or you can add @checkvalue3
    end
    else
    begin
        set @columnlist = @columnlist + ',value2'
        set @columnlistvalue = @columnlistvalue + ',''D1'''
    end
 end

select * from table1
set @sql = 'insert into table1 ('+ @columnlist +') values ( ' + @columnlistvalue + ')'


    select @sql  --you can check that what is wrong by 

exec sp_executesql @sql
select * from table1    

rollback

1 Comment

let say i have more than one input need to be checking before doing insertion, what should i do? @Ajay2707

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.