Solved

Stored procedure with a parameter

Posted on 2016-10-27
6
36 Views
Last Modified: 2016-10-27
Hello,
Need to add a where clause to a stored procedure only if the column is present in the table .
ALTER PROCEDURE  [dbo].[M_SelectMaxVal_FrmCol]
(
     @tableName varchar(100) = null,
	 @ColumnName1 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_]

	--SET @SQL = 'SELECT MAX ('    
        
	--		SET @SQL = @SQL +  '[' +  @ColumnName1 + ']'  + ' +1 ) ' +   + ' FROM  [dbo].[' + @tableName + '] '  		
	
	--  SET @SQL = @SQL  	
	  
	--  EXEC(@SQL)


	  DECLARE @SQL VARCHAR(1000) = null


SET @SQL = '
	
		SELECT 
		
				CASE 
						WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
				ELSE 
				
						MAX ([' +  @ColumnName1 + ']'  + ' +1 ) END Output'  
				
				+ ' FROM  [dbo].[' + @tableName + ']'

			  
EXECUTE(@SQL)
		
   
	
END

Open in new window


Change it to
ALTER PROCEDURE  [dbo].[M_SelectMaxVal_FrmCol]
(
     @tableName varchar(100) = null,
	 @ColumnName1 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_]

	--SET @SQL = 'SELECT MAX ('    
        
	--		SET @SQL = @SQL +  '[' +  @ColumnName1 + ']'  + ' +1 ) ' +   + ' FROM  [dbo].[' + @tableName + '] '  		
	
	--  SET @SQL = @SQL  	
	  
	--  EXEC(@SQL)


	  DECLARE @SQL VARCHAR(1000) = null


SET @SQL = '
	
		SELECT 
		
				CASE 
						WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
				ELSE 
				
						MAX ([' +  @ColumnName1 + ']'  + ' +1 ) END Output'  
				
				+ ' FROM  [dbo].[' + @tableName + ']'


-----Add this only if year_ref column is present in the table 

SET @SQL = @Sql  +'where year_ref ' = '22'
			  
EXECUTE(@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
  • 3
  • 3
6 Comments
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41861873
You'll need to check that column name in the sys.columns view. If exists then add the WHERE clause:
ALTER PROCEDURE  [dbo].[M_SelectMaxVal_FrmCol]
(
 @tableName varchar(100) = null,
 @ColumnName1 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(1000) = null
	
	SET @SQL = 'SELECT CASE 
			WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
			ELSE MAX ([' +  @ColumnName1 + ']'  + ' +1 ) END Output'  
		+ ' FROM  [dbo].[' + @tableName + ']'

	IF EXISTS(SELECT 1
		FROM sys.tables t
			INNER JOIN sys.columns c ON t.object_id=c.object_id
		WHERE t.name = @tableName AND c.name = 'year_ref')
		-----Add this only if year_ref column is present in the table 
		SET @SQL = @Sql  +' where year_ref = 22'
			  
	EXECUTE(@SQL)
	
END

Open in new window

0
 

Author Comment

by:RIAS
ID: 41861884
Thanks Vitor,

Is there a way where where year_ref take current year and checks in the look up table for the number my lookup table is like this:
table _YearLookup
Columns are  year_no and year_ref

0	0
1	1995
2	1996
3	1997
4	1998
5	1999
6	2000
7	2001
8	2002
9	2003
10	2004
11	2005
12	2006
13	2007
14	2008
15	2009
16	2010
17	2011
18	2012
19	2013
20	2014
21	2015
22	2016
23	2017
24	2018

Open in new window


So should return '22' for 2016 and so on
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41861891
Yes, it can be made but you need to provide me the lookup table structure.
Or, since it looks like a mathematic formula (1995-1994=1; 2016-1994=22; 2018-1994=24; ...) you can add immediately the formula to the query:
ALTER PROCEDURE  [dbo].[M_SelectMaxVal_FrmCol]
(
     @tableName varchar(100) = null,
	 @ColumnName1 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(1000) = null
	
	SET @SQL = 'SELECT CASE 
						WHEN CAST(GETDATE() AS DATE) = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) THEN 1 
						ELSE MAX ([' +  @ColumnName1 + ']'  + ' +1 ) END Output'  
			+ ' FROM  [dbo].[' + @tableName + ']'

	IF EXISTS(SELECT 1
			FROM sys.tables t
				INNER JOIN sys.columns c ON t.object_id=c.object_id
			WHERE t.name = @tableName AND c.name = 'year_ref')
		-----Add this only if year_ref column is present in the table 
		SET @SQL = @Sql  +' WHERE year_ref = YEAR(GETDATE())-1994'
			  
	EXECUTE(@SQL)
	
END

Open in new window

0
Independent Software Vendors: 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!

 

Author Comment

by:RIAS
ID: 41861895
Cheers,

This is the query to create the table
ALTER PROCEDURE  [dbo].[SP_TblCreateYEAR_LOOKUP1]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;  

    --Script to create a look up table to dispaly years
	CREATE TABLE YEAR_LOOKUP
		(Year_Ref smallint NOT NULL,
		 Year_no smallint NOT NULL,
		 PRIMARY KEY (Year_Ref))
   
END

Open in new window


Query2 to add values


ALTER PROCEDURE  [dbo].[SP_TblCreateYEAR_LOOKUP1.1]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;  

  
    --Script to insert data in table  Year_Lookup 
		
	INSERT INTO YEAR_LOOKUP (Year_Ref, Year_no) VALUES
				 (0,0)
				,(1,1995),(2,1996),(3,1997),(4,1998)
				,(5,1999), (6,2000),(7,2001 ),(8,2002),(9,2003)
				,(10,2004),(11,2005),(12,2006),(13,2007),(14,2008)
				,(15,2009),(16,2010),(17,2011),(18,2012),(19,2013)
				,(20,2014),(21,2015),(22,2016),(23,2017),(24,2018)
END

Open in new window

0
 

Author Comment

by:RIAS
ID: 41861903
Vitor, Cheers!
It worked like charm!
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41861904
So you won't need to use the lookup table in the SP?
1

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.

Question has a verified solution.

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

Suggested Solutions

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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

739 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