Solved

View Sql in stored procedure

Posted on 2016-10-28
13
20 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 45

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 45

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 45

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 45

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 45

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

759 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

20 Experts available now in Live!

Get 1:1 Help Now