Hi All,
on SQL 2000 can I use a IF statement to execute a dynamic sql. If I get the print statement output to creat tables theyare fine but in the Dynamic sql in the IF statement the tables did not get created. Below is my script
DECLARE @YR varchar(4)
DECLARE @T_Yr varchar(25)
DECLARE @sqlstr varchar(2000)
SET @YR = Year(getdate())
SET @T_Yr = 'Dairy_' + @YR
SET @sqlstr =
' CREATE TABLE ' + @T_Yr + '_T1 (' + Char(10) +
' [id] [int] IDENTITY (1, 1) NOT NULL ,' + Char(10) +
' [r_ID] [int] NULL ,'+ Char(10) +
' [sysobj_name] [varchar] (50) COLLATE '+ Char(10) +
')'+ Char(10) +
'GO'+ Char(10) +
' CREATE TABLE ' + @T_Yr + '_T2 (' + Char(10) +
' [id] [int] IDENTITY (1, 1) NOT NULL ,' + Char(10) +
' [r_ID] [int] NULL ,'+ Char(10) +
' [sysobj_name] [varchar] (50) COLLATE '+ Char(10) +
')'+ Char(10) +
'GO'
If DAY(getdate())> 50
Begin
print @sqlstr
print '-- Create tables'
Exec(@sqlstr)
END
GO
on SQL 2000 can I use a IF statement to execute a dynamic sql. If I get the print statement output to creat tables theyare fine but in the Dynamic sql in the IF statement the tables did not get created. Below is my script
DECLARE @YR varchar(4)
DECLARE @T_Yr varchar(25)
DECLARE @sqlstr varchar(2000)
SET @YR = Year(getdate())
SET @T_Yr = 'Dairy_' + @YR
SET @sqlstr =
' CREATE TABLE ' + @T_Yr + '_T1 (' + Char(10) +
' [id] [int] IDENTITY (1, 1) NOT NULL ,' + Char(10) +
' [r_ID] [int] NULL ,'+ Char(10) +
' [sysobj_name] [varchar] (50) COLLATE '+ Char(10) +
')'+ Char(10) +
'GO'+ Char(10) +
' CREATE TABLE ' + @T_Yr + '_T2 (' + Char(10) +
' [id] [int] IDENTITY (1, 1) NOT NULL ,' + Char(10) +
' [r_ID] [int] NULL ,'+ Char(10) +
' [sysobj_name] [varchar] (50) COLLATE '+ Char(10) +
')'+ Char(10) +
'GO'
If DAY(getdate())> 50
Begin
print @sqlstr
print '-- Create tables'
Exec(@sqlstr)
END
GO