以下是我在SSMS上面打的查詢
但我想不通 nostr為什麼會被判定為null
如果把insert 拿掉直接print會是正常的
DECLARE @count INT;
DECLARE @nostr nvarchar(5);
SET @count = 357;
WHILE @count < 365
BEGIN
select RIGHT(REPLICATE('0', 5) + CAST(@count as nvarchar), 5) as nostr -- [num 361] to [str 00361]
INSERT [dbo].[TxtLink] ([TlinkBlock], [TlinkMsg], [TlinkSort], [TlinkDate]) VALUES (N'901T00004', @nostr, 1, CAST(N'2022-06-29T13:20:57.440' AS DateTime))
--print @nostr;
SET @count = @count + 1;
END;
兩個方法
一個是
SET @nostr = RIGHT(REPLICATE('0', 5) + CAST(@count as nvarchar), 5);
第二個
select @nostr=RIGHT(REPLICATE('0', 5) + CAST(@count as nvarchar), 5)
您宣告了
DECLARE @nostr nvarchar(5)
將字串補0,卻是給了nostr
select RIGHT(REPLICATE('0', 5) + CAST(@count as nvarchar), 5) as nostr
insert時,使用@nostr
insert TxtLink([TlinkMsg]) values (@nostr)