SQL Server database, execute statements across permissions

Source: Internet
Author: User

In the original: SQL Server database, execute statements across permissions

Problem Source: Recently a colleague needs to execute a bulk DELETE statement. Based on his business needs, he is recommended to use the "TRUNCATE TABLE" statement. However, using this statement requires ALTER permission, which does not match the role of the executing user.

WORKAROUND: Use the EXECUTE AS statement to modify execution permissions. The code is as follows:

ALTER PROCEDURE [dbo].[sp_truncatetable]     @TableName varchar( $)      with EXECUTE  as Self as BEGIN     --print out the current execution context user    SELECTNt_username, Loginame fromsys.sysprocessesWHEREspid= @ @SPID    SETNOCOUNT on; DECLARE @TruncateSql nvarchar( -); SET @TruncateSql = 'TRUNCATE TABLE' + @TableName     EXEC(@TruncateSql) END

By printing out the loginame can be seen that the execution of user information has been modified.

SQL Server database, execute statements across permissions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.