Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6048575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:28:43+00:00 2026-05-23T07:28:43+00:00

I have a stored procedure that retrieves sensitive information from an SQL Server 2008

  • 0

I have a stored procedure that retrieves sensitive information from an SQL Server 2008 database. I would like to modify the procedure so that any time it is called, it records information about who called it in a separate table.

I thought something like the following would work:

declare @account varchar(255);
set @account = (SELECT SYSTEM_USER);

INSERT into AUDIT_LOG(ACCOUNT, TSTAMP)
VALUES(@account, getdate())
;

--Now fetch data
SELECT x,y,z from sensitive_info;

My issue is that the client application can issue a call to this stored procedure and get the sensitive information, but not commit the connection and the INSERT never occurs!

Is there some way to force the INSERT to happen before the SELECT?

I am using SQL Server 2008.

Thanks,
Carl

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:28:44+00:00Added an answer on May 23, 2026 at 7:28 am

    You only COMMIT if a transaction has been started.

    So you can test for an open transaction first and disallow the read. This will ensure that no transaction is open to be rolled back. I’ve used XACT_STATE() here

    Using SET XACT_ABORT ON and TRY/CATCH too will mean that the INSERT for logging must happen too before the read happens. Any errors at all on INSERT will go to the CATCH block. So no read and the logging fail can itself be logged too.

    So: this is your guarantee of “read only if logged”

    Having an explicit transaction doesn’t help: the INSERT is an atomic action anyway. And if the called opens a transaction the log entry can be rolled back

    CREATE PROC getSecretStuff
    AS
    SET NOCOUNT, XACT_ABORT ON;
    
    BEGIN TRY
        IF XACT_STATE() <> 0
           RAISERRROR ('Call not allowed in an active transaction', 16, 1)
    
        INSERT into AUDIT_LOG(ACCOUNT, TSTAMP)
        VALUES(SYSTEM_USER, getdate());
    
        --Now fetch data
        SELECT x,y,z from sensitive_info;
    
    END TRY
    BEGIN CATCH
        --     error handling etc
    END CATCH
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that looks like: CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT =
I have a stored procedure that returns values from a temp table. In my
I have a stored procedure that is performing some ddl dml operations. It retrieves
So I have a stored procedure in a SQLServer 2005 database, which retrieves data
i have a stored procedure that has to retrieve data from multiple tables something
I have a stored procedure that returns a valueI call it from other stored
I have a stored procedure that finds all the existing databases and reads from
I am having difficulty executing a MS SQL Server stored procedure from Java/jsp. I
I have a strange, sporadic issue. I have stored procedure that returns back 5
I have a stored procedure that consists of a single select query used to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.