Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Error using If Logic to create specific version of a table. Expand / Collapse
Author
Message
Posted Tuesday, January 29, 2013 8:43 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 14, 2014 6:53 AM
Points: 47, Visits: 244
When I parse the code below, I get the error 'There is already an object named '#DBCC_OUTPUT' in the database.'

I don't want to resort to dynamic SQL if I can avoid it. I didn't include all the columns in the create clause for brevity's sake.

DECLARE @ProdVersion VARCHAR(4)

SELECT @ProdVersion = CASE
WHEN SUBSTRING(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)), 1, 3) = '9.0'
THEN '2005'
WHEN SUBSTRING(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)), 1, 3) = '10.'
THEN '2008'
WHEN SUBSTRING(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)), 1, 3) = '11.'
THEN '2012'
END


IF (@ProdVersion = '2012')
BEGIN
--Create the table with extra columns added in 2012
CREATE TABLE #DBCC_OUTPUT (
[Error] INT NULL
)
END
ELSE
BEGIN
--Create the table with the columns generated in 2005/2008
CREATE TABLE #DBCC_OUTPUT (
[Error] INT NULL
)
END





Post #1413093
Posted Tuesday, January 29, 2013 9:00 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:44 PM
Points: 11,970, Visits: 10,995
The parser doesn't care about logical processing. It sees the same create temp table and throws up its hands. Maybe you can change your logic a little bit to avoid dynamic sql. In your comments it sounds like you have the same columns in 2005/2008 but with 2012 you need to simply add some columns? If so, just create your 2005 version of the table and then add whatever columns you need for 2012.

DECLARE @ProdVersion VARCHAR(4)

SELECT @ProdVersion = CASE
WHEN SUBSTRING(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)), 1, 3) = '9.0'
THEN '2005'
WHEN SUBSTRING(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)), 1, 3) = '10.'
THEN '2008'
WHEN SUBSTRING(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)), 1, 3) = '11.'
THEN '2012'
END


CREATE TABLE #DBCC_OUTPUT
(
[Error] INT NULL
)

IF (@ProdVersion = '2012')
BEGIN
--Alter the table with extra columns added in 2012
alter TABLE #DBCC_OUTPUT
add YourNewColumn varchar(10)

alter TABLE #DBCC_OUTPUT
add YourOtherColumn int
END



_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1413111
Posted Tuesday, January 29, 2013 9:08 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 14, 2014 6:53 AM
Points: 47, Visits: 244
I'm inserting the output of

EXECUTE ('DBCC CHECKDB([' + @DBname + ']) WITH TABLERESULTS');

In 2012 there's additional columns. Trying to make the script I use in 2008 work with 2012 so I only have to maintain 1 script.

I ended up creating 2 different temp tables, based on the version, and then had to use if logic for all of my statements.

Post #1413120
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse