Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 CTE Syntax error

Author  Topic 

emmim44
Yak Posting Veteran

65 Posts

Posted - 2012-08-28 : 04:16:01
Hi guyzz,

I am getting this err:
Msg 102, Level 15, State 1, Line 51
Incorrect syntax near '+ @Cols +'.



declare @SQL nvarchar(max), @Cols nvarchar(max), @Cols1 nvarchar(max)

select @Cols = stuff((

SELECT ',' + quotename(column_name)
FROM information_schema.columns
WHERE table_name = 'unsaccountb'
AND COLUMN_NAME <> 'XObjectKey'
AND COLUMN_NAME NOT LIKE 'UID%' -- exclude columns starting with UID
FOR XML PATH('')),1,1,'')

select @Cols1 = stuff((

SELECT ',CONVERT(Varchar(max),' + quotename(column_name) +
') AS ' + quotename(COLUMN_NAME)
FROM information_schema.columns
WHERE table_name = 'unsaccountb'
AND COLUMN_NAME <> 'XObjectKey'
AND COLUMN_NAME NOT LIKE 'UID%' -- exclude columns starting with UID
FOR XML PATH('')),1,1,'')

---set @SQL = '
;WITH NewValues AS
(
SELECT XObjectKey, FieldName, FieldValue
FROM (select xObjectKey, ' + @Cols1 + ' FROM [unsaccountb] ) Src

UNPIVOT
(
FieldValue FOR FieldName IN ( '+ @Cols +' )

) as x
)

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-28 : 04:19:01
would yoou expect
select xObjectKey, ' + @Cols1 + ' FROM [unsaccountb]

to be valid?
Are yoou trying to create a string to execute? if so yoou need to uncomment the set statement and add a quote at the end.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

emmim44
Yak Posting Veteran

65 Posts

Posted - 2012-08-28 : 04:28:36
quote:
Originally posted by nigelrivett

would yoou expect
select xObjectKey, ' + @Cols1 + ' FROM [unsaccountb]

to be valid?
Are yoou trying to create a string to execute? if so yoou need to uncomment the set statement and add a quote at the end.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.



I dont want to use "set", and i need to create a select statement of "newValues"...
Go to Top of Page
   

- Advertisement -