--execute as login = 'sa';
--execute as user = 'guest';
SELECT @@version
--current context / execute as
, user "user" --same as user_name()
, user_name() "user_name"
, current_user "current_user"
, session_user "session_user"
--current login unless execute as
, system_user "system_user"
, suser_name() "suser_name"
--original context
, ORIGINAL_LOGIN() "original_login"
GO
Friday, April 21, 2017
Thursday, March 23, 2017
Example checking for DML type of trigger
Most of the time it is better to have separate insert/update/delete triggers if you need to do different things based on the DML type. If you did want to have common code then could call a stored procedure with the DML type as a parameter.
In the rare instances where it may be more practical to have one trigger to rule them all, here is an example of how to check for the type of DML activity of the transaction executing the trigger.
ALTER TRIGGER dbo.tr_test_iud
ON dbo.test
AFTER INSERT,UPDATE,DELETE
AS
DECLARE @trgtype varchar(10)
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT TOP 1 1 FROM inserted)
IF EXISTS (SELECT TOP 1 1 FROM deleted) SET @trgtype = 'UPDATE'
ELSE SET @trgtype = 'INSERT'
ELSE SET @trgtype = 'DELETE'
PRINT @trgtype
END
GO
Keep in mind also, it is best practice to always code for there being more than one record involved in the transaction resulting in multiple records in the insert/deleted tables.
In the rare instances where it may be more practical to have one trigger to rule them all, here is an example of how to check for the type of DML activity of the transaction executing the trigger.
ALTER TRIGGER dbo.tr_test_iud
ON dbo.test
AFTER INSERT,UPDATE,DELETE
AS
DECLARE @trgtype varchar(10)
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT TOP 1 1 FROM inserted)
IF EXISTS (SELECT TOP 1 1 FROM deleted) SET @trgtype = 'UPDATE'
ELSE SET @trgtype = 'INSERT'
ELSE SET @trgtype = 'DELETE'
PRINT @trgtype
END
GO
Keep in mind also, it is best practice to always code for there being more than one record involved in the transaction resulting in multiple records in the insert/deleted tables.
Tuesday, March 14, 2017
Server not configured for DATA ACCESS
Error: Server 'SERVER_NAME' is not configured for DATA ACCESS.
Resolution:
exec sp_serveroption 'SERVER_NAME', 'data access', 'true'
go
Resolution:
exec sp_serveroption 'SERVER_NAME', 'data access', 'true'
go
Monday, January 9, 2017
SQL Configuration Manager Cannot connect to WMI provider
Cannot connect to WMI provider. You do not have permission or the server is unreachable. Note that you can only manage SQL Server 2005 and later servers with SQL Server Configuration Manager.
Invalid namespace [0x8004100e]
This problem occurs because the WMI provider is removed when you uninstall an instance of SQL Server. The 32-bit instance and the 64-bit instance of SQL Server share the same WMI configuration file. This file is located in the %programfiles(x86)% folder.
To work around this problem, open a command prompt, type the following command, and then press ENTER:
Note For this command to succeed, the Sqlmgmproviderxpsp2up.mof file must be present in the %programfiles(x86)%\Microsoft SQL Server\number\Shared folder.
mofcomp "%programfiles(x86)%\Microsoft SQL Server\number\Shared\sqlmgmproviderxpsp2up.mof"
The value of number depends on the version of SQL Server:nnn
Microsoft SQL Server 2016 130
Microsoft SQL Server 2014 120
Microsoft SQL Server 2012 110
Microsoft SQL Server 2008 R2 100
Microsoft SQL Server 2008 100
Microsoft SQL Server 2005 90
https://support.microsoft.com/en-us/kb/956013
Invalid namespace [0x8004100e]
This problem occurs because the WMI provider is removed when you uninstall an instance of SQL Server. The 32-bit instance and the 64-bit instance of SQL Server share the same WMI configuration file. This file is located in the %programfiles(x86)% folder.
To work around this problem, open a command prompt, type the following command, and then press ENTER:
Note For this command to succeed, the Sqlmgmproviderxpsp2up.mof file must be present in the %programfiles(x86)%\Microsoft SQL Server\number\Shared folder.
mofcomp "%programfiles(x86)%\Microsoft SQL Server\number\Shared\sqlmgmproviderxpsp2up.mof"
Microsoft SQL Server 2016 130
Microsoft SQL Server 2014 120
Microsoft SQL Server 2012 110
Microsoft SQL Server 2008 R2 100
Microsoft SQL Server 2008 100
Microsoft SQL Server 2005 90
https://support.microsoft.com/en-us/kb/956013
Friday, December 16, 2016
super trim
CREATE FUNCTION dbo.trim( @val nvarchar(max) )
RETURNS nvarchar(max)
BEGIN
DECLARE @tab nchar(1)
, @lf nchar(1)
, @cr nchar(1)
, @crlf nchar(2)
SET @tab = char(9)
SET @lf = char(10)
SET @cr = char(13)
SET @crlf = char(13) + char(10)
RETURN ( replace(replace(replace(replace(ltrim(rtrim(@val)), @tab, ''), @crlf, ''), @lf, ''), @cr, '' ) )
END --trim()
RETURNS nvarchar(max)
BEGIN
DECLARE @tab nchar(1)
, @lf nchar(1)
, @cr nchar(1)
, @crlf nchar(2)
SET @tab = char(9)
SET @lf = char(10)
SET @cr = char(13)
SET @crlf = char(13) + char(10)
RETURN ( replace(replace(replace(replace(ltrim(rtrim(@val)), @tab, ''), @crlf, ''), @lf, ''), @cr, '' ) )
END --trim()
Monday, October 31, 2016
Friday, October 14, 2016
a call to LogonUserW failed
xp_cmdshell raises error "a call to LogonUserW failed with error code 1385"
An error occurred during the execution of xp_cmdshell. A call to 'LogonUserW' failed with error code: '1385'.
In order to fix this you need to open the Local Security Settings on the host machine.
Navigate to Security Settings -> Local Policies -> User Rights Assignment.
Open "Log on as a batch job" and add the user assigned as the xp_cmdshell proxy account
An error occurred during the execution of xp_cmdshell. A call to 'LogonUserW' failed with error code: '1385'.
In order to fix this you need to open the Local Security Settings on the host machine.
Navigate to Security Settings -> Local Policies -> User Rights Assignment.
Open "Log on as a batch job" and add the user assigned as the xp_cmdshell proxy account
Subscribe to:
Posts (Atom)