I am trying to use an inline IF statement in a script to get the first record in a script to load. What I want to happen is for the if statment to check to see if there are no records in the table. Once it sees that the record count = 0, just hard code a 1 for the form_id. What is wrong with the If statement?
IF (select count(*) from dbo.form) = 0
begin
INSERT dbo.Form(form_id, form_description, form_number, edition_date, active, listondec)
SELECT 1, @form_description, @form_number, @edition_date, 1/*Active*/, @listondec
FROM dbo.Form
end
Once the first record is added, the next If statment will handle the rest of the work. From here, it incriments the form number. This IF statment works fine to keep duplicate forms from entering the table:
IF NOT EXISTS(SELECT * FOM dbo.Form WHERE form_number = @form_number)
BEGIN
INSERT dbo.Form(form_id, form_description, form_number, edition_date, active, listondec)
SELECT MAX(form_id) + 1, @form_description, @form_number, @edition_date, 1/*Active*/, @listondec
FROM dbo.Form
END
This is the first time I've worked with inline sql server code. Sorry if it is a basic question.
Thanks,
Ryan
Start Free Trial