Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Trying to create list from separate Sql
Message
From
04/02/2019 17:50:08
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Trying to create list from separate Sql
Environment versions
SQL Server:
SQL Server 2016
Miscellaneous
Thread ID:
01665900
Message ID:
01665900
Views:
70
I'm trying to get a list of values based off a series of sql.

What is going on is

passing in a list of field names for a particular table, parsing that list and then getting the value of those fieldnames to save off
declare
  @KeyFieldValue int = 123456, 
  @FileName varchar(25) = 'SuperVisionLevel',
  @KeyFieldName varchar(10) = 'LevelKey',
  @FileList varchar(300) = 'LevelKey,CountyOffenderKey,SupvLevel,BeginDate,AuditDate,',
  @Name varchar(50),
  @SID varchar(30),
  @PID varchar(30)


declare @cLoc int, @strlen int, @ThisField varchar(25),
        @ColValues varchar(250), @ColCnt int, @ThisValue nvarchar(30),
        @ThisSQL nvarchar(300), @HoldList varchar(300)

set @ColValues = ''
set @ColCnt = 1
set @strlen = len(@FileList)
set @HoldList = @FileList
while @strlen > 0
begin
  set @strlen = len(@FileList)
  set @cloc = charindex(',', @FileList)
  set @ThisField = substring(@FileList, 1, @cloc-1)
  set @FileList = substring(@FileList, @cloc+1, @strlen - (@cloc))
  print @FileList
  set @ThisSQL = '(Select ' +  @ThisField  + ' from ' 
      + @FileName + ' where ' + @KeyFieldName + ' = ' + cast(@KeyFieldValue as Varchar) + ')'
  exec sp_executesql @ThisSQL, @ThisValue out    -- This works and shows result in 'results' tab
print @ThisSQL                                                          -- Nothing prints here
  set @ColValues = @ColValues + ';' + @ThisValue     -- This...not so much, and when
  set @strlen = len(@FileList)
end
Can this work? If so, what am I missing?
"You don't manage people. You manage things - people you lead" Adm. Grace Hopper
Pflugerville, between a Rock and a Weird Place
Next
Reply
Map
View

Click here to load this message in the networking platform