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
Solved

View Sql in stored procedure

Posted on 2016-10-28
13
29 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 48

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 48

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 48

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 48

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 48

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

839 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