?
Solved

How to store executed insert query in the table sql server?

Posted on 2014-07-31
5
Medium Priority
?
177 Views
Last Modified: 2014-10-01
Hi ,

I am having the following stored procedure to insert.. I want to add 'sqlquery' column in the below table to store the executed insert query. How this will be done?
ALTER PROCEDURE [dbo].[usp_InsertItemHold]
        @Description varchar(500),
       @FromDate smalldatetime,
                 @ToDate smalldatetime,
                @ItemHoldStartDate smalldatetime,
            @CreatedOn smalldatetime,
        @ReturnID INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO ItemHold(Description, FromDate,
                      ToDate, ItemHoldStartDate,  CreatedOn)
values
(@Description, @FromDate,
                      @ToDate, @ItemHoldStartDate
                      , @CreatedOn)
SELECT @ReturnID  = @@IDENTITY
0
Comment
Question by:Saroj13
[X]
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
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Chris Watson
ID: 40231998
Why do you want to do that? You're already capturing the inserted data, neatly separated into strongly typed, searchable fields.
0
 

Author Comment

by:Saroj13
ID: 40232002
I want that for future if I need to execute query due to any reasons, I can execute the stored insert query
0
 
LVL 5

Accepted Solution

by:
Chris Watson earned 2000 total points
ID: 40233940
I guess this might work.

ALTER TABLE ItemHold
ADD SqlQuery varchar(max)

Open in new window


ALTER PROCEDURE [dbo].[usp_InsertItemHold]
    @Description varchar(500),
    @FromDate smalldatetime,
    @ToDate smalldatetime,
    @ItemHoldStartDate smalldatetime,
    @CreatedOn smalldatetime,
    @ReturnID INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;

DECLARE @SqlQuery varchar(max)
SET @SqlQuery = 
	'INSERT INTO ItemHold'
  + '(Description'
  + ', FromDate'
  + ', ToDate'
  + ', ItemHoldStartDate'
  + ', CreatedOn)'
  + ' VALUES'
  + '(''' + REPLACE(@Description, '''', '''''') + ''')'
  + ', ''' + CONVERT(varchar(30), @FromDate, 126) + ''''
  + ', ''' + CONVERT(varchar(30), @ToDate, 126) + ''''
  + ', ''' + CONVERT(varchar(30), @ItemHoldStartDate, 126) + ''''
  + ', ''' + CONVERT(varchar(30), @CreatedOn, 126) + ''')'

INSERT INTO ItemHold(Description, FromDate,
                     ToDate, ItemHoldStartDate,
                     CreatedOn, SqlQuery)
VALUES
(@Description, @FromDate,
 @ToDate, @ItemHoldStartDate,
 @CreatedOn, @SqlQuery)
SELECT @ReturnID  = @@IDENTITY 

Open in new window


You could consider creating an audit log (maybe using a trigger) if you're worried about the data getting modified after it's been added.
0
 

Author Comment

by:Saroj13
ID: 40242939
Hi Chris,

Thanks . Its working great. I need little help.

If Description or ItemHoldStartDate or CreatedOn is Null, then insert query is not inserted in the SqlQuery column. Its having null value in sqlquery column


How to fix this?
0
 
LVL 5

Expert Comment

by:Chris Watson
ID: 40243393
You'd have to COALESCE those parameters to account for NULL values.

ALTER PROCEDURE [dbo].[usp_InsertItemHold]
    @Description varchar(500),
    @FromDate smalldatetime,
    @ToDate smalldatetime,
    @ItemHoldStartDate smalldatetime,
    @CreatedOn smalldatetime,
    @ReturnID INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;

DECLARE @SqlQuery varchar(max)
SET @SqlQuery = 
	'INSERT INTO ItemHold'
  + '(Description'
  + ', FromDate'
  + ', ToDate'
  + ', ItemHoldStartDate'
  + ', CreatedOn)'
  + ' VALUES'
  + '(' + COALESCE('''' + REPLACE(@Description, '''', '''''') + '''', 'NULL')
  + ', ' + COALESCE('''' + CONVERT(varchar(30), @FromDate, 126) + '''', 'NULL')
  + ', ' + COALESCE('''' + CONVERT(varchar(30), @ToDate, 126) + '''', 'NULL')
  + ', ' + COALESCE('''' + CONVERT(varchar(30), @ItemHoldStartDate, 126) + '''', 'NULL')
  + ', ' + COALESCE('''' + CONVERT(varchar(30), @CreatedOn, 126) + '''', 'NULL') + ')'

INSERT INTO ItemHold(Description, FromDate,
                     ToDate, ItemHoldStartDate,
                     CreatedOn, SqlQuery)
VALUES
(@Description, @FromDate,
 @ToDate, @ItemHoldStartDate,
 @CreatedOn, @SqlQuery)
SELECT @ReturnID  = @@IDENTITY 

Open in new window

0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article I will describe the Detach & Attach 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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

765 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