sql server TSQL在有字符串拼接的情況下,必須要初始化字符串,
set @errorMsg += N'表 role 沒有主鍵'+'.';如果没有给@errorMsg赋初始值,则select @errorMgs得到的永远是NULL;
declare @errorMsg nvarchar(500); declare @tmpErrorMsg nvarchar(500); declare @pkCount int; declare @PKTable table (TABLE_QUALIFIER varchar(100), TABLE_OWNER varchar(100), TABLE_NAME varchar(100), COLUMN_NAME varchar(100), KEY_SEQ int, PK_NAME varchar(100)); --在有字符串拼接的情況下,必須要初始化字符串 set @errorMsg = N''; insert into @PKTable EXEC sp_pkeys @table_name='role'; set @pkCount=(select COUNT(*) from @PKTable); if(@pkCount <= 0) begin set @errorMsg += N'表 role 沒有主鍵'+'.'; end delete from @PKTable;