Solved

View Sql in stored procedure

Posted on 2016-10-28
13
23 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
  • 8
  • 5
13 Comments
 
LVL 46

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 46

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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 46

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 46

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 46

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 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

16 Experts available now in Live!

Get 1:1 Help Now