Nvarchar和文本在add运算符中不兼容

时间:2021-06-10 17:03:22

With SQL Server 2008, I'd like to add a variable (@wfID) into a text, like below:

使用SQL Server 2008,我想在文本中添加一个变量(@wfID),如下所示:

 DECLARE @wfID uniqueidentifier
 SET @wfID = NEWID()

'<META http-equiv="Content-Type" content="text/html; " />  <br><input type="button"
    value="" onclick="window.open(&quot;http://localhost/TestWeb2/Test_Look.aspx?Test_ID='
    + convert(nvarchar, @wfID)
    + '&quot;);" /></br>',

So, I'd like to add the @wfID to the text but it always says

所以,我想将@wfID添加到文本中,但它总是说

The data types nvarchar and text are incompatible in the add operator.

数据类型nvarchar和text在add运算符中不兼容。

I tried converting everything into nvarchar, but then I got this:

我尝试将所有内容转换为nvarchar,但后来我得到了这个:

Arithmetic overflow error converting expression to data type nvarchar.

将表达式转换为数据类型nvarchar的算术溢出错误。

Any suggestions?

2 个解决方案

#1


22  

In your call to convert you try to convert to just nvarchar which implicitly means nvarchar(1), i.e. there is not room for the whole guid to be converted.

在你的转换调用中,你尝试转换为nvarchar,它隐含地意味着nvarchar(1),即没有空间可以转换整个guid。

Change that to

改为

convert(nvarchar(36), @wfID)

and it will work.

它会起作用。

For some strange reason MSSQL gives an arithmetic overflow if doing SELECT CONVERT(nvarchar,NEWID()) but gives a proper 'Insufficient result space' if doing SELECT CONVERT(varchar,NEWID()).

出于某些奇怪的原因,如果执行SELECT CONVERT(nvarchar,NEWID()),MSSQL会产生算术溢出,但如果执行SELECT CONVERT(varchar,NEWID())则会给出正确的“不足的结果空间”。

#2


-1  

This is working fine'

这工作正常'

    --EXECUTE usp_TEST
    CREATE PROCEDURE [dbo].[usp_TEST](

    @UserID nvarchar(max) ='82F1A3A6-4DC6-481F-9046-856270E66468'
    )
    AS
    BEGIN
    declare @sqlwhere1 nvarchar(max)=''
    SELECT   @sqlwhere1= 'SELECT * 
        FROM [yourtablename] WITH(NOLOCK) WHERE '

        IF(@UserID <> '')
        BEGIN 
         SELECT @sqlwhere1=@sqlwhere1 +' convert(nvarchar(max),UserId)= '''+convert(nvarchar(max),@UserID)+'''' 
        END 

    EXECUTE (@sqlwhere1) 

    END

#1


22  

In your call to convert you try to convert to just nvarchar which implicitly means nvarchar(1), i.e. there is not room for the whole guid to be converted.

在你的转换调用中,你尝试转换为nvarchar,它隐含地意味着nvarchar(1),即没有空间可以转换整个guid。

Change that to

改为

convert(nvarchar(36), @wfID)

and it will work.

它会起作用。

For some strange reason MSSQL gives an arithmetic overflow if doing SELECT CONVERT(nvarchar,NEWID()) but gives a proper 'Insufficient result space' if doing SELECT CONVERT(varchar,NEWID()).

出于某些奇怪的原因,如果执行SELECT CONVERT(nvarchar,NEWID()),MSSQL会产生算术溢出,但如果执行SELECT CONVERT(varchar,NEWID())则会给出正确的“不足的结果空间”。

#2


-1  

This is working fine'

这工作正常'

    --EXECUTE usp_TEST
    CREATE PROCEDURE [dbo].[usp_TEST](

    @UserID nvarchar(max) ='82F1A3A6-4DC6-481F-9046-856270E66468'
    )
    AS
    BEGIN
    declare @sqlwhere1 nvarchar(max)=''
    SELECT   @sqlwhere1= 'SELECT * 
        FROM [yourtablename] WITH(NOLOCK) WHERE '

        IF(@UserID <> '')
        BEGIN 
         SELECT @sqlwhere1=@sqlwhere1 +' convert(nvarchar(max),UserId)= '''+convert(nvarchar(max),@UserID)+'''' 
        END 

    EXECUTE (@sqlwhere1) 

    END