将负值传递给存储过程

时间:2012-01-31 20:10:57

标签: sql-server stored-procedures

我试图在.net中的存储过程中插入ID值,ID的int值在存储过程中插入负值。

但是当传递负值时,它会在'*'附近给出错误的错误语法。

请帮帮我。

这是我的存储过程

ALTER PROCEDURE [dbo].[HotlinePlusAdministration_ArticleMigrator]
        @Id AS INT,
        --@CategoryID AS INT,
        --@Title AS Varchar(200),
        --@ArticleDate AS datetime,
        @DestLinkServer AS VARCHAR(50),
        @UserID     AS VARCHAR(8),
        @ReturnMsg AS VARCHAR(1000) OUTPUT
AS

BEGIN

DECLARE @Query AS NVARCHAR(4000)
DECLARE @Log AS VARCHAR(8000)
DECLARE @ArticleID as int
DECLARE @NewArticleID as int
DECLARE @ArticleKeyExists as int
DECLARE @Title as varchar(200)
DECLARE @CategoryID  as INT
DECLARE @ArticleDate  as varchar(30)
DECLARE @ParmDefinition nvarchar(500);

SET XACT_ABORT ON -- Required for nested transaction
BEGIN TRAN


-- Check if ArticleID exists in Destination Server

  SET @Query =  N'  SELECT    @ArticleKeyExists = COUNT(*)
                       FROM ' +   @DestLinkServer + '.HL2_61.dbo.Article' + ' where ArticleKey = ' + str(@Id)

        SET @ParmDefinition =  N' @ID int, @ArticleKeyExists int OUTPUT';

        EXECUTE sp_executesql @Query , @ParmDefinition, @ID, @ArticleKeyExists  OUTPUT;
--EXECUTE sp_executesql 1234,'BRHLSQL8','BRWSQLDC',@return = retnmsg

                     IF @@ERROR <> 0
                         BEGIN 
                                 ROLLBACK TRANSACTION
                            SET @ReturnMsg = @Log + '<span style="color:red;">ERROR: <br></span>'
                                RETURN -1
                        END


--Delete existing Articles for select page
set @Query = 'DELETE FROM ' + @DestLinkServer + 
            '.HL2_61.dbo.Article ' +
             'WHERE ArticleKey = ' + CONVERT(VARCHAR, @Id)
            --'WHERE CategoryID = ' + CONVERT(VARCHAR, @CategoryID)  + ' and Title = ''' + @Title + '''  and ArticleDate  = ''' + @ArticleDate  + ''''
Print @Query
EXEC(@Query)

当我执行下面的代码时,我收到错误。

DECLARE @return_value int,
        @ReturnMsg varchar(1000)

EXEC    @return_value = [dbo].[Migrator]
        @Id = -1591276581,
        @DestLinkServer = N'SQLDC',
        @UserID = N'10c1',
        @ReturnMsg = @ReturnMsg OUTPUT

SELECT  @ReturnMsg as N'@ReturnMsg'

SELECT  'Return Value' = @return_value

GO

请有人帮帮我..

由于

1 个答案:

答案 0 :(得分:1)

int转换为varchar时,您必须指定尺寸:

试试这个:

CONVERT(VARCHAR(50), @Id) 

并避免使用str(@Id)