RIAS
asked on
How to print the sql query
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?
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_
[Address_2]=CAST(@Address_
[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?
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
ASKER
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 ')
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_
[Address_2]=CAST(@Address_
[PostCode]= CAST(@PostCode AS Nvarchar(50)),
[Sec]=CAST(@Sec AS Nvarchar(50)),
[Comments] =CAST(@Comments AS Nvarchar(50))
WHERE [Dtable_UID] =@Dtable_UID ')
ASKER
I need to insert the executed query in another table
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Vitor,will and get back ..thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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_
[Address_2]=CAST(@Address_
[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
ASKER
Lee Savidge ,
Will try your solution now mate!
Will try your solution now mate!
ASKER
Cheers Experts!Was looking for this for ages ;you are stars!!!!
You're welcome. Glad to be of help :)
ASKER
Lee Savidge,
Any suggestions on how to convert int or date as its throws an error
Any suggestions on how to convert int or date as its throws an error
ASKER
CAST('''+@Year_Ref+''' AS int), ---error
-- CAST('''+ @Date +''' AS varchar(20)), --- error
-- CAST('''+ @Date +''' AS varchar(20)), --- error
You'll need to cast your int as nvarchar() and the date will need converting.
So something like:
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.
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 ')
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.
ASKER
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
(
[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
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.
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),
ASKER
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!
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.