Solved

How to print the sql query

Posted on 2016-08-26
18
49 Views
Last Modified: 2016-08-26
Hello,

Is there a way to print the sql query in stored procedure  which is just executed :

 UPDATE [dbo].[Dtable]
            SET
                  [Name]=CAST(@Name AS  Nvarchar(MAX)),            
              [Title]=CAST(@Title AS  Nvarchar(10)),                        
                  [Field]= CAST(@Field AS  varchar(50)),             
                  [Practice]=CAST(@Practice AS  nvarchar(50)),
                  [Tel_1]=  CAST(@Tel_1 AS Nvarchar(50)),
                  [Tel_2]=CAST(@Tel_2 AS Nvarchar(50)),
                  [Mob]=CAST(@Mob AS Nvarchar(50)),
                  [Fax]=CAST(@Fax AS Nvarchar(50)),
                  [Address_1]=CAST(@Address_1 AS Nvarchar(50)),
                  [Address_2]=CAST(@Address_2 AS Nvarchar(50)),
                  [PostCode]=      CAST(@PostCode AS Nvarchar(50)),
                  [Sec]=CAST(@Sec AS Nvarchar(50)),
                  [Comments]      =CAST(@Comments AS Nvarchar(50))
          WHERE [Dtable_UID] =@Dtable_UID       

need to print this.Any suggestions?
0
Comment
Question by:RIAS
  • 11
  • 5
  • 2
18 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 41771568
If you just want a one off to see what is going to the database, use the SQL Tracing tool which is called SQL Server Profiler. Hopefully it was installed.

When you run it, create a new trace and then in the second tab, choose Show All Columns, and then scroll across the the database name, click the column heading and in the "Like", expand it and put in your database name.

Then run the trace. Then in your application, you can run the bit that executes your stored procedure and once you've done it, stop the trace.

Then use the Find tool and look for your stored procedure name and you'll see what it was executed with.
1
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41771585
Well, you need to use copy the UPDATE command and send it to PRINT before execute it:
CREATE PROC sp_MySP (@Name...)
AS

PRINT 'UPDATE [dbo].[Dtable]
             SET 
                   [Name]=CAST(@Name AS  Nvarchar(MAX)),             
               [Title]=CAST(@Title AS  Nvarchar(10)),                        
                   [Field]= CAST(@Field AS  varchar(50)),             
                   [Practice]=CAST(@Practice AS  nvarchar(50)),
                   [Tel_1]=  CAST(@Tel_1 AS Nvarchar(50)),
                   [Tel_2]=CAST(@Tel_2 AS Nvarchar(50)),
                   [Mob]=CAST(@Mob AS Nvarchar(50)),
                   [Fax]=CAST(@Fax AS Nvarchar(50)),
                   [Address_1]=CAST(@Address_1 AS Nvarchar(50)),
                   [Address_2]=CAST(@Address_2 AS Nvarchar(50)),
                   [PostCode]=      CAST(@PostCode AS Nvarchar(50)),
                   [Sec]=CAST(@Sec AS Nvarchar(50)),
                   [Comments]      =CAST(@Comments AS Nvarchar(50))
           WHERE [Dtable_UID] =@Dtable_UID '

 UPDATE [dbo].[Dtable]
             SET 
                   [Name]=CAST(@Name AS  Nvarchar(MAX)),             
               [Title]=CAST(@Title AS  Nvarchar(10)),                        
                   [Field]= CAST(@Field AS  varchar(50)),             
                   [Practice]=CAST(@Practice AS  nvarchar(50)),
                   [Tel_1]=  CAST(@Tel_1 AS Nvarchar(50)),
                   [Tel_2]=CAST(@Tel_2 AS Nvarchar(50)),
                   [Mob]=CAST(@Mob AS Nvarchar(50)),
                   [Fax]=CAST(@Fax AS Nvarchar(50)),
                   [Address_1]=CAST(@Address_1 AS Nvarchar(50)),
                   [Address_2]=CAST(@Address_2 AS Nvarchar(50)),
                   [PostCode]=      CAST(@PostCode AS Nvarchar(50)),
                   [Sec]=CAST(@Sec AS Nvarchar(50)),
                   [Comments]      =CAST(@Comments AS Nvarchar(50))
           WHERE [Dtable_UID] =@Dtable_UID 

RETURN

Open in new window

0
 

Author Comment

by:RIAS
ID: 41771597
Thanks Vitor,

Is it the correct way I can
      INSERT INTO tblDEBUG (SQL) VALUES (PRINT 'UPDATE [dbo].[Dtable]
             SET
                   [Name]=CAST(@Name AS  Nvarchar(MAX)),            
               [Title]=CAST(@Title AS  Nvarchar(10)),                        
                   [Field]= CAST(@Field AS  varchar(50)),            
                   [Practice]=CAST(@Practice AS  nvarchar(50)),
                   [Tel_1]=  CAST(@Tel_1 AS Nvarchar(50)),
                   [Tel_2]=CAST(@Tel_2 AS Nvarchar(50)),
                   [Mob]=CAST(@Mob AS Nvarchar(50)),
                   [Fax]=CAST(@Fax AS Nvarchar(50)),
                   [Address_1]=CAST(@Address_1 AS Nvarchar(50)),
                   [Address_2]=CAST(@Address_2 AS Nvarchar(50)),
                   [PostCode]=      CAST(@PostCode AS Nvarchar(50)),
                   [Sec]=CAST(@Sec AS Nvarchar(50)),
                   [Comments]      =CAST(@Comments AS Nvarchar(50))
           WHERE [Dtable_UID] =@Dtable_UID ')
0
 

Author Comment

by:RIAS
ID: 41771600
I need to insert the executed query in another table
0
 
LVL 46

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 100 total points
ID: 41771605
If instead of PRINT to screen you want to insert into another, then you must do it as you presented, with the INSERT command but remove the PRINT:
INSERT INTO tblDEBUG (SQL) 
VALUES ('UPDATE [dbo].[Dtable]
              SET 
                    [Name]=CAST(@Name AS  Nvarchar(MAX)),             
                [Title]=CAST(@Title AS  Nvarchar(10)),                        
                    [Field]= CAST(@Field AS  varchar(50)),             
                    [Practice]=CAST(@Practice AS  nvarchar(50)),
                    [Tel_1]=  CAST(@Tel_1 AS Nvarchar(50)),
                    [Tel_2]=CAST(@Tel_2 AS Nvarchar(50)),
                    [Mob]=CAST(@Mob AS Nvarchar(50)),
                    [Fax]=CAST(@Fax AS Nvarchar(50)),
                    [Address_1]=CAST(@Address_1 AS Nvarchar(50)),
                    [Address_2]=CAST(@Address_2 AS Nvarchar(50)),
                    [PostCode]= CAST(@PostCode AS Nvarchar(50)),
                    [Sec]=CAST(@Sec AS Nvarchar(50)),
                    [Comments]=CAST(@Comments AS Nvarchar(50))
            WHERE [Dtable_UID] =@Dtable_UID ') 

Open in new window

1
 

Author Comment

by:RIAS
ID: 41771613
Vitor,will and get back ..thanks
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 400 total points
ID: 41771614
Assuming you have the tblDebug then:

insert into tblDebug (myQueryColumn) values('UPDATE [dbo].[Dtable]
             SET 
                   [Name]=CAST(''' + @Name + ''' AS  Nvarchar(MAX)),             

.... 

           WHERE [Dtable_UID] =@Dtable_UID ')

Open in new window


This way you will see the update statement with the data. You'll need to fill in the other lines the same way. You may have to do some type conversions in there.
0
 

Author Comment

by:RIAS
ID: 41771628
Hello Vitor ,
tried it but does not give values .This the copy of data inserted in the table


 UPDATE [dbo].[Dtable]
            SET
                  [Name]=CAST(@Name AS  Nvarchar(MAX)),            
              [Title]=CAST(@Title AS  Nvarchar(10)),                        
                  [Field]= CAST(@Field AS  varchar(50)),             
                  [Practice]=CAST(@Practice AS  nvarchar(50)),
                  [Tel_1]=  CAST(@Tel_1 AS Nvarchar(50)),
                  [Tel_2]=CAST(@Tel_2 AS Nvarchar(50)),
                  [Mob]=CAST(@Mob AS Nvarchar(50)),
                  [Fax]=CAST(@Fax AS Nvarchar(50)),
                  [Address_1]=CAST(@Address_1 AS Nvarchar(50)),
                  [Address_2]=CAST(@Address_2 AS Nvarchar(50)),
                  [PostCode]=      CAST(@PostCode AS Nvarchar(50)),
                  [Sec]=CAST(@Sec AS Nvarchar(50)),
                  [Comments]      =CAST(@Comments AS Nvarchar(50))
          WHERE [Dtable_UID] =@Dtable_UID

no values of @ are inserted
0
 

Author Comment

by:RIAS
ID: 41771635
Lee Savidge ,
Will try your solution now mate!
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Closing Comment

by:RIAS
ID: 41771652
Cheers Experts!Was looking for this for ages ;you are stars!!!!
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 41771721
You're welcome. Glad to be of help :)
0
 

Author Comment

by:RIAS
ID: 41771747
Lee Savidge,

Any suggestions on how to convert int or date as its throws an error
0
 

Author Comment

by:RIAS
ID: 41771749
CAST('''+@Year_Ref+'''  AS int),      ---error
--    CAST('''+ @Date +'''  AS  varchar(20)), --- error
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 41771783
You'll need to cast your int as nvarchar() and the date will need converting.

So something like:

insert into tblDebug (myQueryColumn) values('UPDATE [dbo].[Dtable]
             SET 
                   [Name]=CAST(''' + @Name + ''' AS  Nvarchar(MAX)),
                   [myNumber] = cast(''' + cast(@myNumber as nvarchar(10)) + ''' as nvarchar(10)),
                   [myDate] = cast(''' + cast(@mydate as nvarchar(25)) + ''' as nvarchar(25)),
...


           WHERE [Dtable_UID] =@Dtable_UID ')

Open in new window


Something like that? If you need more help, you may need to post another question but if/when you do, try and provide some example data of what you have and what you expect.
0
 

Author Comment

by:RIAS
ID: 41771796
INSERT INTO Table1(SQL) VALUES ( 'INSERT INTO table2
            (
                  [Ref],
                  [Auth],
                  [Date],
                  [Year_Ref],      
           
                  [FaxAuth],            
                  [Attachments],
                  [FaxNo],
                  [Attn],                  
                [FaxLetterMemo],
                  [To],
                [Regarding],
                  [SentBy]
            )
            VALUES
            (
                  CAST(''' +@Ref + '''  AS  Nvarchar(50)),
                  CAST('''+@Auth+''' AS NVARCHAR(255)),      
                CAST('''+ @Date +'''  AS  varchar(20)),
                  CAST('''+@Year_Ref+'''  AS INTEGER),      
     
                  CAST('''+@FaxAuth +''' AS Nvarchar(255)),                  
                  CAST('''+ @Attachments+''' AS Nvarchar(255)),
                  CAST('''+@FaxNo+''' AS Nvarchar(255)),                        
                  CAST('''+@Attn+''' AS Nvarchar(255)),                              
                  CAST('''+@FaxLetterMemo +''' AS Nvarchar(255)),
                  CAST('''+@To +''' AS Nvarchar(255)),
                  CAST('''+@Regarding +''' AS Nvarchar(255)),
                  CAST('''+@SentBy+''' AS   Nvarchar(255)))'

The bold are the problem thanks
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 41771809
They both depend on the initial data types of the variables. I will assume @Date is a datetime. If so, and you want the whole datetime string then you'll need a bigger varchar than 20 as it will be in the form:

yyyy-mm-dd hh:mi:ss.mmm

which is 23 chars, so 25 is better. If you only really want to capture the date then you can truncate the time. I'll assume you want the full datetime.

CAST('''+ convert(nvarchar(25), @date, 121) + '''  AS  varchar(25)),
CAST('''+ cast(@Year_Ref as nvarchar(4)) +'''  AS INTEGER),

Open in new window

0
 

Author Comment

by:RIAS
ID: 41771818
Lee you are truly Genius!  As you suggested I have posted a new question.Can you please copy this comment there I can give you points!
0
 

Author Comment

by:RIAS
ID: 41771820
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
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…
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.

914 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

19 Experts available now in Live!

Get 1:1 Help Now