【標題】:SQL varchar varchar(n)
【日期】:2016-07-04
【類別】:SQLSERVER
【問題】:T-SQL 聲明變量類型爲varchar後執行插入語句插入不成功
【緣由】:聲明變量varchar不帶長度,sqlserver會默認長度1代替
http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-declaring-varchar-without-length.aspx
【怎麼發現的】:sql
declare @f_accounts varchar set @f_accounts='H6155' MERGE INTO t_OtherCredit AS T using (SELECT @f_accounts AS f_accounts) AS S ON (T.f_accounts = S.f_accounts AND Datediff(d, T.f_date, Getdate()) = 0) WHEN NOT MATCHED THEN INSERT (f_accounts, f_date) VALUES(@f_accounts, Getdate()) ; --執行T-SQL後查詢 SELECT * FROM t_OtherCredit WHERE Datediff(d, f_date, Getdate()) = 0 AND f_accounts = 'H6154'
無數據,然而,直接替換@f_accounts爲字面量查詢到正常數據sqlserver
【修復】:declare @f_accounts varchar(20)spa
【在哪些文件修改了】: 無code
【我致使的】:是server
【解決Bug的時間】:2016-07-04 13:10blog
【教訓】:sqlserver仍是簡單瞭解階段it