Solved

Stored procedure

Posted on 2016-09-26
4
40 Views
Last Modified: 2016-09-26
Hello,

Can't get this Sp working. Any suggestions?


CREATE PROCEDURE  [dbo].[Tel_WordReport]
(
     @tableName varchar(100) = null,
	 @ColumnName1 varchar(100) = null,
	 @Value varchar(100) = null
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;  	
	
	   declare @SQL varchar(500) = null
	  -- NONCLUSTERED INDEX [NIX__UNQ__UID_]
	   DECLARE @Year_Ref smallint
		SET @Year_Ref = (SELECT [Year_Ref] FROM YEAR_LOOKUP  WHERE [Year_no]= YEAR(GETDATE()))

	



	SET @SQL = 'SELECT Date,Ref, Comments,SentTo,SentBy,Regarding'
		
		SET @SQL = @SQL + ' FROM  [' + @tableName + '] '   + ' WHERE  [' + @ColumnName1 + ']  = ''' +   '' +  @Value +''''+ ' AND  [Year_Ref] =''' + '23 '+''''
	
	
	
	  SET @SQL = @SQL  	
	  
	  EXEC (@SQL)	
   
	
END

Open in new window

0
Comment
Question by:RIAS
[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
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41815502
the Year_Ref was hard coded with space value?

>>AND  [Year_Ref] =''' + '23 '+''''

from your SP, you will have query like this:
SELECT Date,Ref, Comments,SentTo,SentBy,Regarding FROM  [yourTable]  WHERE  [yourField]  = 'yourValue' AND  [Year_Ref] ='23 '

Open in new window

make sure it's a valid statement for your DB.
0
 

Author Comment

by:RIAS
ID: 41815510
Thanks Ryan,
How can I avoid hard coded value and  use
  DECLARE @Year_Ref smallint
            SET @Year_Ref = (SELECT [Year_Ref] FROM YEAR_LOOKUP  WHERE [Year_no]= YEAR(GETDATE()))
0
 
LVL 52

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41815514
quick try:
ALTER PROCEDURE  [dbo].[Tel_WordReport]
(
     @tableName varchar(100) = null,
	 @ColumnName1 varchar(100) = null,
	 @Value varchar(100) = null
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;  	
	
	   declare @SQL varchar(500) = null
	  -- NONCLUSTERED INDEX [NIX__UNQ__UID_]
	   DECLARE @Year_Ref smallint
		SET @Year_Ref = (SELECT [Year_Ref] FROM YEAR_LOOKUP  WHERE [Year_no]= YEAR(GETDATE()))

	SET @SQL = 'SELECT Date,Ref, Comments,SentTo,SentBy,Regarding'
		
		SET @SQL = @SQL + ' FROM  [' + @tableName + '] '   + ' WHERE  [' + @ColumnName1 + ']  = ''' +   '' +  @Value +''''+ ' AND  [Year_Ref] =' + convert(varchar,@Year_Ref)
	
	  SET @SQL = @SQL  	
	  
	  EXEC (@SQL)	
	
END

Open in new window

1
 

Author Closing Comment

by:RIAS
ID: 41815522
Cheers mate!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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