jwe
Starting Member
13 Posts |
Posted - 2007-06-26 : 10:50:47
|
Hi I wrote a cursor for the part of the dynamic SQL that is too long for varchar(8000). It all works fine but I have to put this into a stored procedure.When I run the code below with the print statements it give me what I want ie.CREATE VIEW DBO.auditEVENT_DeletedAS SELECT Col.*, CM.Created, CM.Modified, DD.Deleted FROM (SELECT PrimaryKey, Max(Case ColumnName WHEN [EventID] THEN LastValue ELSE '' END) AS [EventID] Max(Case ColumnName WHEN [EntryDate] THEN LastValue ELSE '' END) AS [EntryDate] Max(Case ColumnName WHEN [NotifiedDate] THEN LastValue ELSE '' END) AS [NotifiedDate] Max(Case ColumnName WHEN [ArrivedDate] THEN LastValue ELSE '' END) AS [ArrivedDate] Max(Case ColumnName WHEN [StartedDate] THEN LastValue ELSE '' END) AS [StartedDate]Etc.I want to execute @SQL1+@SQL2 at the end but because I am using a cursor it comes out like this:CREATE VIEW DBO.auditEVENT_DeletedAS SELECT Col.*, CM.Created, CM.Modified, DD.Deleted FROM (SELECT PrimaryKey, Max(Case ColumnName WHEN [EventID] THEN LastValue ELSE '' END) AS [EventID]CREATE VIEW DBO.auditEVENT_DeletedAS SELECT Col.*, CM.Created, CM.Modified, DD.Deleted FROM (SELECT PrimaryKey, Max(Case ColumnName WHEN [EntryDate] THEN LastValue ELSE '' END) AS [EntryDate]CREATE VIEW DBO.auditEVENT_DeletedAS SELECT Col.*, CM.Created, CM.Modified, DD.Deleted FROM (SELECT PrimaryKey, Max(Case ColumnName WHEN [NotifiedDate] THEN LastValue ELSE '' END) AS [NotifiedDate]CREATE VIEW DBO.auditEVENT_DeletedAS SELECT Col.*, CM.Created, CM.Modified, DD.Deleted FROM (SELECT PrimaryKey, Max(Case ColumnName WHEN [ArrivedDate] THEN LastValue ELSE '' END) AS [ArrivedDate]CREATE VIEW DBO.auditEVENT_DeletedAS SELECT Col.*, CM.Created, CM.Modified, DD.Deleted FROM (SELECT PrimaryKey, Max(Case ColumnName WHEN [StartedDate] THEN LastValue ELSE '' END) AS [StartedDate]Etc.How can I achieve the result I get with the print statements when using exec SQL?---------------- code ---------------------SET NOCOUNT ONDECLARE @SQL1 NVARCHAR (1000), @SQL2 NVARCHAR (4000), @column SYSNAMESET @SQL1 = 'CREATE VIEW ' + 'DBO' + '.audit' + 'EVENT' + '_Deleted' + Char(13) + Char(10) + 'AS ' + Char(13) + Char(10) + 'SELECT Col.*, CM.Created, CM.Modified, DD.Deleted FROM ' + ' (SELECT PrimaryKey' + Char(13) + Char(10) + ','PRINT @SQL1declare @columnvalues table(id int, string SYSNAME)insert into @columnvaluesselect distinct c.colid, c.name from sysobjects as t join syscolumns as c on t.id = c.id join systypes as ty on ty.usertype = c.usertype join systypes st on ty.type= st.type where t.name = 'Event' AND c.name NOT IN ('created', 'modified','RowVersion') AND c.iscomputed = 0 AND st.name IN ('tinyint', 'smallint', 'int', 'money', 'smallmoney', 'decimal', 'bigint', 'datetime', 'smalldateteime', 'numeric', 'varchar', 'nvarchar', 'char', 'nchar', 'bit')order by c.colidDECLARE CURS CURSOR STATIC LOCAL FOR select string from @columnvaluesOPEN CURSWHILE 1 = 1BEGIN FETCH CURS INTO @columnIF @@FETCH_STATUS <> 0 BREAKSELECT @SQL2 =' Max(Case ColumnName WHEN ' + quotename(@column) + ' THEN LastValue ELSE '''' END) AS ' + quotename(@column)PRINT @SQL2ENDDEALLOCATE CURSSET NOCOUNT OFF |
|