I've a large dynamic sql statement built in asp. I need to return an error (or anything) from within the exec sql statement. I've tried as shown below but return does not work within the string, and @@ERROR outside the exec statement reports no errors. I've been looking at the forum and the usual answer seems to be to use sp_executesql - in this case, is this possible - can the sql string be concatenated in the same way as with exec(str1+str2)? This must be simple surely?! Thanks in advance.
The sql:
CREATE PROCEDURE usp_MyProc
@strsql1 varchar(8000),
@strsql2 varchar(8000)
AS
EXEC (@strsql1 + @strsql2 +
'IF @@ERROR=0
BEGIN
COMMIT TRANSACTION
RETURN @@ERROR
END
ELSE
BEGIN
ROLLBACK TRANSACTION
RETURN @@ERROR
END ')
GO
The sql:
CREATE PROCEDURE usp_MyProc
@strsql1 varchar(8000),
@strsql2 varchar(8000)
AS
EXEC (@strsql1 + @strsql2 +
'IF @@ERROR=0
BEGIN
COMMIT TRANSACTION
RETURN @@ERROR
END
ELSE
BEGIN
ROLLBACK TRANSACTION
RETURN @@ERROR
END ')
GO