Solved

use parameters in where clause with in a sql string

Posted on 2011-09-07
3
263 Views
Last Modified: 2012-05-12
Hiya don't know if the title is correct but this is my code -

I'm trying to add parameters in the where clause of this query, i have successfully managed to add the database parameter but cant seem to get the where clause to work with parameters. I don't really want to run the where clause outside of this as takes longer when putting results into tmp table then using parameters.
Thanks
if OBJECT_ID('tempdb..##tmpUserDBase') IS NOT NULL
	begin 
	   drop 
	   table       ##tmpUserDBase
	end


declare @dbase varchar(150) = 'testdbase'
declare @ncode varchar(25) = 'DT10150'
declare @period varchar(10) = 8
declare @year varchar(10) = 'C'
declare @STR varchar(max)


SET @STR =' 
		 SELECT '''+@dbase+''' as [DBASE]
			,[DET_NOMINALDR] as [Nominal Code]
                        ,[DET_DATE] as [Date]
                        ,[DET_PERIODNUMBR] as [Period]
                        ,[DET_YEAR] as [Year]
                        ,[DET_DESCRIPTION] as [Narrative]
                        ,[DET_NETT] as [D/C]

           FROM ' + @dbase + '.[dbo].[SL_PL_NL_DETAIL]
          WHERE [DET_NOMINALDR] = ''@ncode''
            and [DET_PERIODNUMBR] = ''@period''
            and [DET_YEAR] = ''@year''
            '
			 
  
create table ##tmpUserDBase
	(DBASE                  varchar(100)
	 ,[Nominal Code]	varchar(100)
         ,[Date]		date
	 ,[Period]		varchar(100)
         ,[Year]		varchar(1)
	 ,[Narrative]		varchar(250)
	 ,[D/C]			float)
				  
	insert into ##tmpUserDBase
	exec (@STR)
	
	
	select * from ##tmpUserDBase

Open in new window

0
Comment
Question by:deanmachine333
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 400 total points
ID: 36496096
Please try the following:

if OBJECT_ID('tempdb..##tmpUserDBase') IS NOT NULL
	begin 
	   drop 
	   table       ##tmpUserDBase
	end


declare @dbase varchar(150) = 'testdbase'
declare @ncode varchar(25) = 'DT10150'
declare @period varchar(10) = 8
declare @year varchar(10) = 'C'
declare @STR varchar(max)


SET @STR =' 
		 SELECT '''+@dbase+''' as [DBASE]
			,[DET_NOMINALDR] as [Nominal Code]
                        ,[DET_DATE] as [Date]
                        ,[DET_PERIODNUMBR] as [Period]
                        ,[DET_YEAR] as [Year]
                        ,[DET_DESCRIPTION] as [Narrative]
                        ,[DET_NETT] as [D/C]

           FROM ' + @dbase + '.[dbo].[SL_PL_NL_DETAIL]
          WHERE [DET_NOMINALDR] = ''' + @ncode + '''
            and [DET_PERIODNUMBR] = ''' + @period + '''
            and [DET_YEAR] = ''' + @year + '''
            '
			 
  
create table ##tmpUserDBase
	(DBASE                  varchar(100)
	 ,[Nominal Code]	varchar(100)
         ,[Date]		date
	 ,[Period]		varchar(100)
         ,[Year]		varchar(1)
	 ,[Narrative]		varchar(250)
	 ,[D/C]			float)
				  
	insert into ##tmpUserDBase
	exec (@STR)
	
	
	select * from ##tmpUserDBase

Open in new window

0
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 100 total points
ID: 36496118
You may need to concatinate the values of the params instead of their names:


SET @STR ='
                 SELECT '''+@dbase+''' as [DBASE]
                        ,[DET_NOMINALDR] as [Nominal Code]
                        ,[DET_DATE] as [Date]
                        ,[DET_PERIODNUMBR] as [Period]
                        ,[DET_YEAR] as [Year]
                        ,[DET_DESCRIPTION] as [Narrative]
                        ,[DET_NETT] as [D/C]

           FROM ' + @dbase + '.[dbo].[SL_PL_NL_DETAIL]
          WHERE [DET_NOMINALDR] = ''' + @ncode + '''
            and [DET_PERIODNUMBR] = ''' + @period + '''
            and [DET_YEAR] = ''' +@year + '''
            '
0
 

Author Closing Comment

by:deanmachine333
ID: 36496202
Thanks guys , i did try that before but didnt do it on the @year , oh and giving points to both but most point were given to wdosanjos as he posted it first.

thanks again.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now