I am trying to insert ID values in stored procedure from .net and the int value for ID is inserting negative value in the stored procedure.
But when a negative value is passed its giving me an error incorrect syntax near ‘*’.
Please help me.
Here is my stored procedure
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)
When I am executing the code as below I am getting the error.
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
Please someone help me..
Thanks
When you convert an
intto avarchar, you have to specify the size:Try this:
and avoid using
str(@Id)