?
Solved

Stored procedure

Posted on 2016-09-26
4
Medium Priority
?
43 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 53

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 53

Accepted Solution

by:
Ryan Chong earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In this article I will describe the Backup & Restore 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.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

771 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