Advertisement

04.16.2008 at 07:30AM PDT, ID: 23327228
[x]
Attachment Details

sql server 2005 inline IF statement

Asked by dchau12 in SQL Server 2005

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,

RyanStart Free Trial
 
Loading Advertisement...
 
[+][-]04.16.2008 at 07:37AM PDT, ID: 21368231

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 07:39AM PDT, ID: 21368255

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 07:39AM PDT, ID: 21368259

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 07:40AM PDT, ID: 21368268

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 07:41AM PDT, ID: 21368280

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 07:41AM PDT, ID: 21368290

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 07:49AM PDT, ID: 21368392

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 07:51AM PDT, ID: 21368416

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: SQL Server 2005
Sign Up Now!
Solution Provided By: chapmandew
Participating Experts: 4
Solution Grade: A
 
 
[+][-]04.16.2008 at 07:52AM PDT, ID: 21368420

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.16.2008 at 07:53AM PDT, ID: 21368443

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628