?
Solved

how can I store my select statement in a variable?

Posted on 2008-10-17
2
Medium Priority
?
464 Views
Last Modified: 2012-05-05
Hello,
Platform MS SQL 2005
I have a stored proc that recieves an xml string builds a temp table and performs an inner join. It works fine. But I want to also send a regular string variable that contains the fields for the select statement. After searching around it seems I can' t do this the way I want, but is there a way to put this into an exec or something so I cna make this work?



create PROCEDURE [dbo].[sp_BattleRoster_SelDataExtract_xmlinput](@upclist xml, @selectstring varchar(max)) AS
DECLARE @units TABLE (ID varchar(20)) 
INSERT INTO @units (ID) SELECT ParamValues.ID.value('.','VARCHAR(20)')
FROM @upclist.nodes('//upc') as ParamValues(ID) 
SELECT   @selectstring   FROM [urodata].[dbo].[vw_BattleRosterDataExtract]
INNER JOIN     @units u
ON    [vw_BattleRosterDataExtract] .upc = u.ID

Open in new window

0
Comment
Question by:Dustin Hopkins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
brad2575 earned 2000 total points
ID: 22744184
Yea do this

EXEC('
INSERT INTO ' + @units  + '(ID) SELECT ' + @SelectFields + '
FROM ' + @upclist + nodes('//upc') + ' as ParamValues(ID)
SELECT  ' +  @selectstring   ' + FROM [urodata].[dbo].[vw_BattleRosterDataExtract]
INNER JOIN     ' + @units + ' u
ON    [vw_BattleRosterDataExtract] .upc = u.ID
')

I am not familar with the XML coding so you may need to adjust the above a bit but you just create a string inside the EXEC statement with quotes around it and then use the "+" anywhere you want to join a variable (like above).  If you want to use a single quote INSIDE the statement then you would use two single quotes (escape characters).



0
 
LVL 14

Author Comment

by:Dustin Hopkins
ID: 22887304
Alright, sorry for the delay, I've been rather busy. I had to do some more research in order to properly incorporate the xml variables and such. Thanks brad for puting me on the right path. Here is the final code.
create PROCEDURE [dbo].[sp_BattleRoster_SelDataExtract_xmlinput]
(@upclist xml, @selectstring varchar(max)) 
AS
DECLARE @SQLStatement varchar(max)
create table #units (ID varchar(20)) 
 
INSERT INTO #units(ID) SELECT  ParamValues.ID.value('.','VARCHAR(20)')
FROM @upclist.nodes('//upc') as ParamValues(ID) 
select @SQLStatement = 'SELECT ' + @selectstring + 
' FROM [urodata].[dbo].[vw_BattleRosterDataExtract]' +
' INNER JOIN #units u ' +
'ON    [vw_BattleRosterDataExtract].upc = u.ID'
--Execute Statement
exec(@SQLStatement)
drop table #units

Open in new window

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question