PROBLEM: Receive error message:
Server: Msg 105, Level 15, State 1, Line 128
Unclosed quotation mark before the character string 'Un
into NewTable FROM (select tbl_ecom_productdescriptor
_idvaluepa
irs.produc
t, tlkp_ecom_productdescripto
r.dscname,
tbl_ecom_productdescriptor
_idvaluepa
irs.dscval
from tbl_ecom_productdescriptor
_idvaluepa
irs inner join tlkp_ecom_productdescripto
r on tbl_ecom_productdescriptor
_idvaluepa
irs.dscid = tlkp_ecom_productdescripto
r.dscid) a
GROUP ...
Server: Msg 170, Level 15, State 1, Line 128
Line 128: Incorrect syntax near 'Un
into NewTable FROM (select tbl_ecom_productdescriptor
_idvaluepa
irs.produc
t, tlkp_ecom_productdescripto
r.dscname,
tbl_ecom_pr'.
--------------------------
--
--------------------------
--
I am running the following dynamic sql:
declare
@table as varchar(500),
@onrows as varchar(128),
@onrowsalias as sysname ,
@oncols as varchar(128),
@sumcol as sysname
set @table=
'(select tbl_ecom_productdescriptor
_idvaluepa
irs.produc
t, tlkp_ecom_productdescripto
r.dscname,
tbl_ecom_productdescriptor
_idvaluepa
irs.dscval
' +
'from tbl_ecom_productdescriptor
_idvaluepa
irs inner join '+
'tlkp_ecom_productdescript
or on tbl_ecom_productdescriptor
_idvaluepa
irs.dscid = tlkp_ecom_productdescripto
r.dscid) a '
set @onrows ='product'
set @onrowsalias= null;
set @oncols='dscname'
set @sumcol='dscval'
declare
@sql as varchar(8000),
@newline as char(1)
set @newline = char(10)
-- step 1: beginning of sql string
set @sql =
'select' + @newline +
' ' + @onrows +
case
when @onrowsalias is not null then ' as ' + @onrowsalias
else ''
end
create table #keys(keyvalue nvarchar(100) not null primary key)
--declare @keys table(keyvalue varchar(100) not null primary key)
declare @keyssql as varchar(1000)
set @keyssql =
'insert into #keys ' +
'select distinct cast(' + @oncols + ' as nvarchar(100)) ' +
'from ' + @table
exec (@keyssql)
declare @key as nvarchar(100)
select @key = min(keyvalue) from #keys
while @key is not null
begin
set @sql = @sql + ',' + @newline +
' max(case cast(' + @oncols +
' as nvarchar(100))' + @newline +
' when N''' + @key +
''' then ' + case
when @sumcol is null then '1'
else @sumcol
end + @newline +
' else null' + @newline +
' end) as ' + @key
select @key = min(keyvalue) from #keys
where keyvalue > @key
end
SET @sql = @sql + @NEWLINE +
' into NewTable FROM ' + @table + @NEWLINE +
'GROUP BY ' + @onrows + @NEWLINE +
'ORDER BY ' + @onrows
-- set @sql = @sql + @newline +
-- 'from ' + @table + @newline +
-- 'group by ' + @onrows + @newline +
-- 'order by ' + @onrows
-- for debugging...this information will print into the messages section
-- of query analyzer...prints information to the messages window at the
-- bottom of the screen...
print @sql + @newline
exec (@sql)