--将两者协调一致,再重启 SQL Server 服务
if serverproperty('servername')<>@@servername
begin
declare @server sysname
set @server= @@servername
exec sys.sp_dropserver @server = @server
set @server = cast(serverproperty('servername') as sysname)
exec sys.sp_addserver @server = @server ,@local = 'LOCAL'
end
use master
go
select name ,@@servername,serverproperty('servername')
from sys.servers
where server_id=0 --Local Server ID = 0
go
2,返回当前module的ID,module包括:SP,UDF,Trigger
@@PROCID
--获取当前Module Name
declare @ObjectName sysname;
select @ObjectName=object_name(@@ProcID)
3,返回当前Session的ID,当前的RequestID
INSERT INTO player (name, surname, info )
VALUES (N'Ovidiu', N'Cracium', COMPRESS(N'{"sport":"Tennis","age": 28,"rank":1,"points":15258, turn":17}'));
在查询数据时,解压缩数据,将数据从varbinary(max)强转为原始类型
SELECT _id, name, surname, datemodified, CAST(DECOMPRESS(info) AS NVARCHAR(MAX)) AS info
FROM player; 五,调试函数
1,获取异常消息
在TSQL中,使用try 和 catch编写异常处理代码,在catch子句中,使用debug函数,能够获取异常信息
-- SET XACT_ABORT ON will render the transaction uncommittable when the constraint violation occurs.
SET XACT_ABORT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- A FOREIGN KEY constraint exists on this table. This statement will generate a constraint violation error.
DELETE FROM Production.Product
WHERE ProductID = 980;
-- If the delete operation succeeds, commit the transaction. The CATCH block will not execute.
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
-- Test XACT_STATE for 0, 1, or -1.
-- If 1, the transaction is committable.
-- If -1, the transaction is uncommittable and should be rolled back.
-- XACT_STATE = 0 means there is no transaction and a commit or rollback operation would generate an error.
-- Test whether the transaction is uncommittable.
IF (XACT_STATE()) = -1
BEGIN
--Logging Exception info, as the transaction is in an uncommittable state. Rolling back transaction.
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
ROLLBACK TRANSACTION;
END;
-- Test whether the transaction is active and valid.
IF (XACT_STATE()) = 1
BEGIN
--'The transaction is committable. Committing transaction.'
COMMIT TRANSACTION;
END;
END CATCH;
GO
View Code 2,抛出异常消息
在SQL Server 2012及之后的版本中,使用 Throw 关键字代替RaiseError,用于抛出异常,并将执行控制权转移到Catch 代码块