Solved

Stored procedure

Posted on 2016-09-26
4
26 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
  • 2
  • 2
4 Comments
 
LVL 49

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 49

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

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.

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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