• zilla (12/18/2012)


    I finally got around to trying some of this stuff this morning. I figured using the IF EXISTS should be pretty easy to put in place.

    Here is the new code.

    IF EXISTS ('SELECT version_number

    FROM '+ ''+@DB_Name+'' +'.dbo.schema_version

    WHERE version_number LIKE ''4.%'' AND version_number NOT LIKE ''4.3%''')

    BEGIN

    ....stuff

    END

    It kicks off his error though: Incorrect syntax near 'SELECT version_number

    FROM '.

    I am sure I probably have a quote out of place somewhere....

    I guess you didn't understand the point we are all making about this. Just because you put together a string it is NOT going to automagically execute that string. Nowhere in your code do you execute this string. All you are looking at is a string, it does not pull any data from the database. Look closely at the two examples posted. The first is using the sp_msforeachdb and the second is using pure dynamic sql. In both cases there is an EXEC. That is what executes the query you built. You just can't do it the way you are trying.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/