String or binary data would be truncated. The statement has been terminated

后端 未结 5 1831
臣服心动
臣服心动 2020-12-29 01:51

I have met some problem with the SQL server, this is the function I created:

ALTER FUNCTION [dbo].[testing1](@price int)
RETURNS @trackingItems1 TABLE (
   i         


        
5条回答
  •  有刺的猬
    2020-12-29 02:44

    When you define varchar etc without a length, the default is 1.

    When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.

    So, if you expect 400 bytes in the @trackingItems1 column from stock, use nvarchar(400).

    Otherwise, you are trying to fit >1 character into nvarchar(1) = fail

    As a comment, this is bad use of table value function too because it is "multi statement". It can be written like this and it will run better

    ALTER FUNCTION [dbo].[testing1](@price int)
    RETURNS
    AS
       SELECT ta.item, ta.warehouse, ta.price 
       FROM   stock ta
       WHERE  ta.price >= @price;
    

    Of course, you could just use a normal SELECT statement..

提交回复
热议问题