Solved

View Sql in stored procedure

Posted on 2016-10-28
13
34 Views
Last Modified: 2016-10-28
Hello,

How can I view this sql in the stored procedure
  IF NOT EXISTS( SELECT Ref
					FROM [CLIENT_REFERENCE] WHERE(					
				
				    ([OrgAuth] = @OrgAuth OR 
					([OrgAuth] is null AND @OrgAuth is null) OR
					([OrgAuth] is null AND @OrgAuth ='') OR
					([OrgAuth] ='' AND @OrgAuth is null))	
	
	   AND 
					([ClientName] = @ClientName OR 
					([ClientName] is null AND @ClientName is null) OR
					([ClientName] is null AND @ClientName ='') OR
					([ClientName] ='' AND @ClientName is null))
	   AND 
					([Sex] = @Sex OR 
					([Sex] is null AND @Sex is null) OR
					([Sex] is null AND @Sex ='') OR
					([Sex] ='' AND @Sex is null))					
	   AND 
					([Comments] = @Comments OR 
					([Comments] is null AND @Comments is null) OR
					([Comments] is null AND @Comments ='') OR
					([Comments] ='' AND @Comments is null))
       AND 
					([Field1] = @Field1 OR 
					([Field1] is null AND @Field1 is null) OR
					([Field1] is null AND @Field1 ='') OR
					([Field1] ='' AND @Field1 is null))))
	

Open in new window


Setting
DECLARE @SQL NVARCHAR(MAX)

    SET @SQL ='

allows me to view the sql .
Any suggestion on the above query
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
  • 8
  • 5
13 Comments
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41863874
Sorry but I don't understand what do you pretend.
Can you rephrase please?
0
 

Author Comment

by:RIAS
ID: 41863877
Need to see the query when I debug the stored procedure.

Sql = "If exists ..."

When you set the query in set sql = we can view it or else there is no way we can debug it.
How can i set the above query as set sql =
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41863882
Still not sure if I got it.
If you run the command PRINT @SQL it will show you the query.
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: 41863885
Yes agreed but , how do I print this query ?
IF NOT EXISTS( SELECT Ref
					FROM [CLIENT_REFERENCE] WHERE(					
				
				    ([OrgAuth] = @OrgAuth OR 
					([OrgAuth] is null AND @OrgAuth is null) OR
					([OrgAuth] is null AND @OrgAuth ='') OR
					([OrgAuth] ='' AND @OrgAuth is null))	
	
	   AND 
					([ClientName] = @ClientName OR 
					([ClientName] is null AND @ClientName is null) OR
					([ClientName] is null AND @ClientName ='') OR
					([ClientName] ='' AND @ClientName is null))
	   AND 
					([Sex] = @Sex OR 
					([Sex] is null AND @Sex is null) OR
					([Sex] is null AND @Sex ='') OR
					([Sex] ='' AND @Sex is null))					
	   AND 
					([Comments] = @Comments OR 
					([Comments] is null AND @Comments is null) OR
					([Comments] is null AND @Comments ='') OR
					([Comments] ='' AND @Comments is null))
       AND 
					([Field1] = @Field1 OR 
					([Field1] is null AND @Field1 is null) OR
					([Field1] is null AND @Field1 ='') OR
					([Field1] ='' AND @Field1 is null))))

Open in new window

0
 

Author Comment

by:RIAS
ID: 41863891
Entire Sp is
ALTER PROCEDURE  [dbo].[TL_AdClieference]





  
    @Ref nvarchar(50) = NULL,	@OrgAuth nvarchar(3) = NULL,		@ClientName nvarchar(255) = NULL,
		@Sex [nvarchar](1) = NULL,	@Comments nvarchar(max)=  NULL,	@Field1 nvarchar(50)= NULL
	
	  
         
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;  

   
 


				
					
	
		IF NOT EXISTS( SELECT Ref
					FROM [CLIENT_REFERENCE] WHERE(					
				
				    ([OrgAuth] = @OrgAuth OR 
					([OrgAuth] is null AND @OrgAuth is null) OR
					([OrgAuth] is null AND @OrgAuth ='') OR
					([OrgAuth] ='' AND @OrgAuth is null))	
	
	   AND 
					([ClientName] = @ClientName OR 
					([ClientName] is null AND @ClientName is null) OR
					([ClientName] is null AND @ClientName ='') OR
					([ClientName] ='' AND @ClientName is null))
	   AND 
					([Sex] = @Sex OR 
					([Sex] is null AND @Sex is null) OR
					([Sex] is null AND @Sex ='') OR
					([Sex] ='' AND @Sex is null))					
	   AND 
					([Comments] = @Comments OR 
					([Comments] is null AND @Comments is null) OR
					([Comments] is null AND @Comments ='') OR
					([Comments] ='' AND @Comments is null))
       AND 
					([Field1] = @Field1 OR 
					([Field1] is null AND @Field1 is null) OR
					([Field1] is null AND @Field1 ='') OR
					([Field1] ='' AND @Field1 is null))))
	
	BEGIN 
	 DECLARE @SQL NVARCHAR(MAX)


    SET @SQL ='	INSERT INTO [dbo].[CLIENT_REFERENCE] ( '

  	
    IF  @Ref <> ''
            SET @SQL = @SQL + '  [Ref] '	
			
    IF  @OrgAuth <> ''
            SET @SQL =@SQL + ',' + '  [OrgAuth]  ' 			 
    
    IF  @ClientName <> ''
            SET @SQL =@SQL + ',' + '  [ClientName]  ' 			 
  		 
     IF  @Sex <> ''
            SET @SQL =@SQL + ',' + '  [Sex]  ' 			 
     IF  @Comments <> ''
            SET @SQL =@SQL + ',' + '  [Comments] ' 			 
     IF  @Field1 <> ''
            SET @SQL =@SQL + ',' + '  [Field1]  ' 			 
    
	 SET @SQL = @SQL +')' + '      
     
     VALUES
	 ('

	 	
	    IF  @Ref <> ''
            SET @SQL =@SQL + '''' +   CAST(@Ref  AS  Nvarchar(50))  + '''' 		 
     	IF  @OrgAuth <> ''
            SET @SQL = @SQL + ' , ''' +  CAST(@OrgAuth   AS  Nvarchar(255))  + ''''
	  
   		IF  @ClientName <> ''
            SET @SQL = @SQL + ' , ''' +  CAST(@ClientName   AS  Nvarchar(20))  + '''' 		
	 		
		IF  @Sex  <> ''
            SET @SQL = @SQL + ' , ''' +  CAST(@Sex   AS  Nvarchar(1))  + '''' 	
	    IF  @Comments  <> ''
            SET @SQL = @SQL + ' , ''' +  CAST(@Comments    AS  Nvarchar(50))  + ''''  
	    IF  @Field1  <> ''
            SET @SQL = @SQL + ' , ''' +  CAST(@Field1  AS  Nvarchar(50))  + '''' 
	   
		
	  SET @SQL =  @SQL + ')'

       EXECUTE (@SQL)
	     INSERT INTO TRAVEL_DEBUG(SQL) VALUES (@SQL)
		Return @@Rowcount
	END
	
	ELSE 	
	BEGIN
	
	    Return 2
	END
	END

Open in new window

0
 

Author Comment

by:RIAS
ID: 41863893
need to print or view
 IF NOT EXISTS( SELECT Ref
					FROM [CLIENT_REFERENCE] WHERE(					
				
				    ([OrgAuth] = @OrgAuth OR 
					([OrgAuth] is null AND @OrgAuth is null) OR
					([OrgAuth] is null AND @OrgAuth ='') OR
					([OrgAuth] ='' AND @OrgAuth is null))	
	
	   AND 
					([ClientName] = @ClientName OR 
					([ClientName] is null AND @ClientName is null) OR
					([ClientName] is null AND @ClientName ='') OR
					([ClientName] ='' AND @ClientName is null))
	   AND 
					([Sex] = @Sex OR 
					([Sex] is null AND @Sex is null) OR
					([Sex] is null AND @Sex ='') OR
					([Sex] ='' AND @Sex is null))					
	   AND 
					([Comments] = @Comments OR 
					([Comments] is null AND @Comments is null) OR
					([Comments] is null AND @Comments ='') OR
					([Comments] ='' AND @Comments is null))
       AND 
					([Field1] = @Field1 OR 
					([Field1] is null AND @Field1 is null) OR
					([Field1] is null AND @Field1 ='') OR
					([Field1] ='' AND @Field1 is null))))
	

Open in new window

0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41863947
You can't print a query by itself.
However you can print a string and that's what you do when you deal with dynamic queries because you built it with a string variable and then execute the string content that's a T-SQL command.
0
 

Author Comment

by:RIAS
ID: 41863951
Thats the question Vitor ,
How can I set this query to string?
0
 

Author Comment

by:RIAS
ID: 41863962
example :


Set sql =  IF NOT EXISTS( SELECT Ref
                              FROM [CLIENT_REFERENCE] WHERE(                              
                        
                            ([OrgAuth] = @OrgAuth OR
                              ([OrgAuth] is null AND @OrgAuth is null) OR
                              ([OrgAuth] is null AND @OrgAuth ='') OR
                              ([OrgAuth] ='' AND @OrgAuth is null))      
      
         AND
                              ([ClientName] = @ClientName OR
                              ([ClientName] is null AND @ClientName is null) OR
                              ([ClientName] is null AND @ClientName ='') OR
                              ([ClientName] ='' AND @ClientName is null))
         AND
                              ([Sex] = @Sex OR
                              ([Sex] is null AND @Sex is null) OR
                              ([Sex] is null AND @Sex ='') OR
                              ([Sex] ='' AND @Sex is null))                              
         AND
                              ([Comments] = @Comments OR
                              ([Comments] is null AND @Comments is null) OR
                              ([Comments] is null AND @Comments ='') OR
                              ([Comments] ='' AND @Comments is null))
       AND
                              ([Field1] = @Field1 OR
                              ([Field1] is null AND @Field1 is null) OR
                              ([Field1] is null AND @Field1 ='') OR
                              ([Field1] ='' AND @Field1 is null))))
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41863967
Ok, got it.
The following should work assuming that the variables are all strings:
DECLARE @SQL NVARCHAR(MAX)

SET @SQL ='
	IF NOT EXISTS( SELECT Ref
				FROM [CLIENT_REFERENCE] 
				WHERE (					
				    ([OrgAuth] = ' + @OrgAuth + ' OR 
					([OrgAuth] is null AND ' + @OrgAuth + ' is null) OR
					([OrgAuth] is null AND ' + @OrgAuth + ' ='''') OR
					([OrgAuth] ='''' AND ' + @OrgAuth + 'is null))	
				AND 
					([ClientName] = ' + @ClientName + ' OR 
					([ClientName] is null AND ' + @ClientName + ' is null) OR
					([ClientName] is null AND ' + @ClientName + ' ='''') OR
					([ClientName] ='''' AND ' + @ClientName + ' is null))
				AND 
					([Sex] = ' + @Sex + ' OR 
					([Sex] is null AND ' + @Sex + ' is null) OR
					([Sex] is null AND ' + @Sex + ' ='''') OR
					([Sex] ='''' AND ' + @Sex + ' is null))					
				AND 
					([Comments] = ' + @Comments + ' OR 
					([Comments] is null AND ' + @Comments + ' is null) OR
					([Comments] is null AND ' + @Comments + ' ='''') OR
					([Comments] ='''' AND ' + @Comments + ' is null))
				AND 
					([Field1] = ' + @Field1 + ' OR 
					([Field1] is null AND ' + @Field1 + ' is null) OR
					([Field1] is null AND ' + @Field1 + ' ='''') OR
					([Field1] ='''' AND ' + @Field1 + ' is null))))'

PRINT @SQL

Open in new window

1
 

Author Comment

by:RIAS
ID: 41863968
Cool! Will try now and get back
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41863988
Btw, you can tune your query like this:
DECLARE @SQL NVARCHAR(MAX)=NULL

SET @OrgAuth=ISNULL(@OrgAuth,'')
SET @ClientName=ISNULL(@ClientName,'')
SET @Sex=ISNULL(@Sex,'')
SET @Comments=ISNULL(@Comments,'')
SET @Field1=ISNULL(@Field1,'')

SET @SQL ='
	IF NOT EXISTS (SELECT Ref
		FROM [CLIENT_REFERENCE] 
		WHERE ([OrgAuth] = ' + @OrgAuth + ' OR (ISNULL([OrgAuth],'''') is null AND ' + @OrgAuth + ' ='''')
		  AND ([ClientName] = ' + @ClientName + ' OR (ISNULL([ClientName],'''') ='''' AND ' + @ClientName + ' ='''')
		  AND ([Sex] = ' + @Sex + ' OR (ISNULL([Sex],'''') ='''' AND ' + @Sex + ' ='''')
		  AND ([Comments] = ' + @Comments + ' OR (ISNULL([Comments],'''') ='''' AND ' + @Comments + ' ='''')
		  AND ([Field1] = ' + @Field1 + ' OR (ISNULL([Field1],'''') ='''' AND ' + @Field1 + ' ='''')'

PRINT @SQL

Open in new window

1
 

Author Closing Comment

by:RIAS
ID: 41864016
Thanks ,minor syntax issues but,will fix it.Thanks for the solution
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

752 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