Frage

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

 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

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

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

Arithmetic overflow error converting expression to data type nvarchar.

Any suggestions?

War es hilfreich?

Lösung

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.

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()).

Andere Tipps

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top