question

megamanblue avatar image
megamanblue asked

Problems with Control of Flow... Confused.com

Hi All, I have been battling with this for a day now and can not figure out what I am doing wrong. The code below is to be executed accross an estate of approximately 200 SQL servers with versions ranging from SQL Server 2000 to SQL Server 2008 R2. Everytime I run this it seems to result IF statements as true irrespective of whether or not they actually are. Any advice on how to get this conditionally executing would be greatly appreciate. Code below. Thanks, MMb To be clearer, I get the following error message when executing on SQL 2000: *Msg 207, Level 16, State 3, Line 4 Invalid column name 'EntryDateTime'.* IF CAST(SERVERPROPERTY('ProductVersion') As VARCHAR(20)) LIKE '8.%' CREATE TABLE #ErrorLogData(TextData VARCHAR(2000),[Contin] VARCHAR(10)) GO IF CAST(SERVERPROPERTY('ProductVersion') As VARCHAR(20)) NOT LIKE '8.%' CREATE TABLE #ErrorLogData([EntryDateTime] VARCHAR(2000),[ProcessInfo] VARCHAR(100),TextData VARCHAR(2000)) GO INSERT INTO #ErrorLogData EXEC sp_readerrorlog GO IF (SELECT CAST(SERVERPROPERTY('ProductVersion') As VARCHAR(20))) LIKE '8.%' SELECT TextData FROM #ErrorLogData WHERE (TextData NOT LIKE '%0 errors%' AND TextData NOT LIKE '%0 transactions%') AND (TextData LIKE '%[1-9]%occurence(s) of IO requests%' OR TextData LIKE '%transactions rolled%' OR TextData LIKE '%fail%' OR TextData LIKE '%error%' OR TextData LIKE '%cannot%' /*OR TextData LIKE '%bypass%'*/) IF (SELECT CAST(SERVERPROPERTY('ProductVersion') As VARCHAR(20))) NOT LIKE '8.%' SELECT EntryDateTime , TextData FROM #ErrorLogData WHERE (TextData NOT LIKE '%0 errors%' AND TextData NOT LIKE '%0 transactions%') AND (TextData LIKE '%[1-9]%occurence(s) of IO requests%' OR TextData LIKE '%transactions rolled%' OR TextData LIKE '%fail%' OR TextData LIKE '%error%' OR TextData LIKE '%cannot%' /*OR TextData LIKE '%bypass%'*/) DROP TABLE #ErrorLogData
t-sqlqueryconditional
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
Which if statement is seemingly returning true? What version of server is it running on when it isn't behaving?
0 Likes 0 ·

1 Answer

·
Fatherjack avatar image
Fatherjack answered
your create statements are different in the first two IF's SELECT CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)) IF CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)) LIKE '8.%' CREATE TABLE #ErrorLogData ( TextData VARCHAR(2000) , [Contin] VARCHAR(10) ) GO IF CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)) NOT LIKE '8.%' CREATE TABLE #ErrorLogData ( [EntryDateTime] VARCHAR(2000) , [ProcessInfo] VARCHAR(100) , TextData VARCHAR(2000) ) GO I would correct this and then test again. certainly the parser would have difficulty working out how to insert into a column that may of may not exist. You may be able to get around this by using different tables that have different structures rather than re-using the same name. [Edit] It was lunchtime so I got a version that works on 2000, 2005 + 2008 (but test it yourself too) -- make sure objects dont exist IF OBJECT_ID('tempdb..#ErrorLogData1') > 0 DROP TABLE #ErrorLogData1 ; IF OBJECT_ID('tempdb..#ErrorLogData2') > 0 DROP TABLE #ErrorLogData2 ; -- test for version, create table, insert data and select resultset IF CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(20)) LIKE '8.%' BEGIN RAISERROR('Found 8',1,1,1) WITH NOWAIT CREATE TABLE #ErrorLogData1 ( TextData VARCHAR(2000) , [Contin] VARCHAR(10) ) INSERT INTO #ErrorLogData1 EXEC sp_readerrorlog ; SELECT TextData FROM #ErrorLogData1 WHERE ( TextData NOT LIKE '%0 errors%' AND TextData NOT LIKE '%0 transactions%' ) AND ( TextData LIKE '%[1-9]%occurence(s) of IO requests%' OR TextData LIKE '%transactions rolled%' OR TextData LIKE '%fail%' OR TextData LIKE '%error%' OR TextData LIKE '%cannot%' /*OR TextData LIKE '%bypass%'*/ ) END ELSE BEGIN RAISERROR('Later than 8',0,0,1) WITH NOWAIT CREATE TABLE #ErrorLogData2 ( [EntryDateTime] VARCHAR(2000) , [ProcessInfo] VARCHAR(100) , TextData VARCHAR(2000) ) INSERT INTO #ErrorLogData2 EXEC sp_readerrorlog ; SELECT EntryDateTime , TextData FROM #ErrorLogData2 WHERE ( TextData NOT LIKE '%0 errors%' AND TextData NOT LIKE '%0 transactions%' ) AND ( TextData LIKE '%[1-9]%occurence(s) of IO requests%' OR TextData LIKE '%transactions rolled%' OR TextData LIKE '%fail%' OR TextData LIKE '%error%' OR TextData LIKE '%cannot%' /*OR TextData LIKE '%bypass%'*/ ) END -- tidy up IF OBJECT_ID('tempdb..#ErrorLogData1') > 0 DROP TABLE #ErrorLogData1 ; IF OBJECT_ID('tempdb..#ErrorLogData2') > 0 DROP TABLE #ErrorLogData2 ;
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

megamanblue avatar image megamanblue commented ·
@Fatherjack Thanks a lot. this has worked I made one very minor tweak to the code as I was receiving the following error on 2005: Msg 2756, Level 16, State 1, Line 33 Invalid value 0 for state. Valid range is from 1 to 127. Thanks again
0 Likes 0 ·
Fatherjack avatar image Fatherjack ♦♦ commented ·
Ah yes! it should be `RAISERROR('Later than 8',1,1,1) WITH NOWAIT ` in the code. apologies for that. Glad you are getting the results you need
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.