Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert simple query to dyanmic SQL - results to table variable/temp table

Posted on 2009-05-07
7
Medium Priority
?
501 Views
Last Modified: 2012-05-06
OK - I have the below query - need to convert it into dynamic SQL with the @datStart and @datStop date time params as dynamic variables - also need to take the results and put them into a table variable or a temp table to be used later on in the sproc - any help would be greatly appreciated!
SELECT @datStart, @datStop
-- DROP TABLE #TEMP_ACCT DROP TABLE #TEMP_AVG
SELECT DISTINCT ACCT_NO INTO #TEMP_ACCT FROM data8.dbStPetes.dbo.tblHEAD
WHERE (ISNULL(DISCHARGE,ADMIT) >= @datStart) AND (ISNULL(DISCHARGE,ADMIT) <= @datStop)
 
SELECT DISTINCT DEPT_CODE, PROC_CODE,
      CASE WHEN ISNULL(SUM(QTY),0) = 0 OR ISNULL(SUM(REV),0) =0 THEN 0
            ELSE SUM(REV)/SUM(QTY)
            END AS AVGREV 
INTO #TEMP_AVG
FROM data8.dbStPetes.dbo.tblTRANS
WHERE ACCT_NO IN(SELECT ACCT_NO FROM #TEMP_ACCT)
GROUP BY DEPT_CODE, PROC_CODE

Open in new window

0
Comment
Question by:tbaseflug
[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
7 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 24326545
ummm...not sure what you mean.  Ifyou just want to use params, then you don't need to use dynamic sql
0
 
LVL 4

Expert Comment

by:bljak
ID: 24326559
I do not quite understand what is your question here.
Except that @datStart and @datStop should be stored procedure parameters your query is already dynamic and is having result in the temp table for later use.
0
 

Author Comment

by:tbaseflug
ID: 24326568
Well - the database name is ultimately going to be a param as well - that I know how to do but am horrible at getting the dates to work and in getting the results out of the dynamic query
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 24326729
Hello tbaseflug,


DECLARE @dbName nvarchar(100) ,@datStart datetime , @datStop datetime
SELECT @dbName ='data8'
SELECT @datStart = '2008-01-01' , @datStop = '2009-01-01'
-- DROP TABLE #TEMP_ACCT DROP TABLE #TEMP_AVG
CREATE TABLE #TEMP_AVG (DEPT_CODE int , PROC_CODE int, AVGREV int )
DECLARE @nSql nvarchar(1000)
SELECT @nSql = ' SELECT DISTINCT ACCT_NO FROM ' +@dbName+ '.dbStPetes.dbo.tblHEAD
WHERE (ISNULL(DISCHARGE,ADMIT) >= @datStart) AND (ISNULL(DISCHARGE,ADMIT) <= @datStop)'

SELECT @nSql = 'SELECT DISTINCT DEPT_CODE, PROC_CODE,
      CASE WHEN ISNULL(SUM(QTY),0) = 0 OR ISNULL(SUM(REV),0) =0 THEN 0
            ELSE SUM(REV)/SUM(QTY)
            END AS AVGREV
FROM '+@dbName+'.dbStPetes.dbo.tblTRANS
WHERE ACCT_NO IN('+@nSql + ')
GROUP BY DEPT_CODE, PROC_CODE'

INSERT INTO #TEMP_AVG
exec sp_ExecuteSQL @nSql, '@datStart datetime, @datStop datetime ', @datStart, @datStop
 

Regards,

Aneesh
0
 

Author Comment

by:tbaseflug
ID: 24326805
OK - I took the above and plugged it in but am getting the following error:
(1 row(s) affected)
Msg 214, Level 16, State 3, Procedure sp_executesql, Line 1
Procedure expects parameter '@parameters' of type 'ntext/nchar/nvarchar'.

DECLARE @dbName nvarchar(100) ,@datStart datetime , @datStop datetime, @cdmDate datetime 
SET @dbName = 'dbStPetes'
SET @cdmDate = '11/15/2006'
 
DECLARE @dateTable TABLE 
(
  cdmType varchar(50), cdmDesc varchar(100), qtyDateFrom datetime,  qtyDateThru datetime,  priorDate datetime
)
 
INSERT INTO @dateTable
EXEC dbo.spWebCDMEditor_GetCDMInfo @dbName, @cdmDate
 
SET @datStart = (SELECT qtyDateFrom FROM @dateTable)  -- begin discharge date
SET @datStop = (SELECT qtyDateThru FROM @dateTable)   -- end discharge date
 
-- DROP TABLE #TEMP_ACCT DROP TABLE #TEMP_AVG
CREATE TABLE #TEMP_AVG (DEPT_CODE int , PROC_CODE int, AVGREV int )
DECLARE @nSql nvarchar(1000) 
SELECT @nSql = ' SELECT DISTINCT ACCT_NO FROM data8.' +@dbName+ '.dbo.tblHEAD
WHERE (ISNULL(DISCHARGE,ADMIT) >= @datStart) AND (ISNULL(DISCHARGE,ADMIT) <= @datStop)' 
 
SELECT @nSql = 'SELECT DISTINCT DEPT_CODE, PROC_CODE,
      CASE WHEN ISNULL(SUM(QTY),0) = 0 OR ISNULL(SUM(REV),0) =0 THEN 0
            ELSE SUM(REV)/SUM(QTY)
            END AS AVGREV 
FROM data8.' +@dbName+ '.dbo.tblTRANS
WHERE ACCT_NO IN('+@nSql + ')
GROUP BY DEPT_CODE, PROC_CODE' 
 
INSERT INTO #TEMP_AVG
exec sp_ExecuteSQL @nSql, '@datStart datetime, @datStop datetime ', @datStart, @datStop

Open in new window

0
 

Author Closing Comment

by:tbaseflug
ID: 31579027
this did it - just changed out end to: exec sp_ExecuteSQL @nSql, N'@datStart datetime, @datStop datetime ', @datStart, @datStop
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24326878
change the last two lines like this

INSERT INTO #TEMP_AVG
exec sp_ExecuteSQL @nSql, N'@datStart datetime, @datStop datetime ', @datStart, @datStop
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

618 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