Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 return the error in the stored procedure

Author  Topic 

wided
Posting Yak Master

218 Posts

Posted - 2011-11-24 : 05:25:22
hello

I use several stocked procedures

I want to know the code that allows me to return an error in my application, for example:

definition of mytable: col1, col2, col3, col4

my code : Insert into mytable
values ??(@ valcol1, valcol2 @, @ valcol3)

in this case I insert 3 values ??when my table has 4 columns and in my case I do not Presice columns

I wish to address the error and return the application and complete the transaction

Now, the system displays:

the number of columns inserted does not match the table definition.

I know I can fix, but I have a lot of stored procedures and I want a system that returns me the error no advance in the treatment






RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2011-11-24 : 05:44:09
Use a TRY..CATCH block and look at ERROR_NUMBER() and ERROR_MESSAGE()
Go to Top of Page

wided
Posting Yak Master

218 Posts

Posted - 2011-11-24 : 08:54:58
Tanks TickD

it works with divion by zero
it works with insert null value

it does not work with:
(the table testcatc has only 2 columns)
BEGIN TRY
-- insert col+++++.
insert into dbo.testcatch values(1, 'toto', 'toto2')

END TRY


BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber;
print 'error column'
END CATCH;
GO







Go to Top of Page
   

- Advertisement -