I have encapsulated a backup database command in a Try/Catch and it appears that the error message is being lost somewhere. For example:
BACKUP DATABASE NonExistantDB TO DISK = 'C:\TEMP\NonExistantDB.bak'
..gives error:
Could not locate entry in sysdatabases for database 'NonExistantDB'. No entry found with that name. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally.
Whereas:
BEGIN TRY
BACKUP DATABASE NonExistantDB TO DISK = 'C:\TEMP\NonExistantDB.bak'
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH
... only gives error: BACKUP DATABASE is terminating abnormally.
Is there a way to get the full error message or is this a limitation of try/catch?
It's a limitation of try/catch.
If you look carefully at the error generated by executing
BACKUP DATABASE NonExistantDB TO DISK = 'C:\TEMP\NonExistantDB.bak'
you'll find that there are two errors that get thrown. The first is msg 911, which states
Could not locate entry in sysdatabases for database 'NonExistantDB'. No entry
found with that name. Make sure that the name is entered correctly.
The second is the 3013 message that you are displaying. Basically, SQL is only returning the last error.
It is a limitation, that I just ran into myself, of the try/catch block in SQL 2005. I don't know if it still exists or not in 2008.
SQL 2005 Error Handling
Related
The ERROR_MESSAGE() is capturing only the last message in the error stack but nothing preceding it. But I want to capture all the errors that is seen on SSMS when a query fails to write it to my Log Table.
In my SQL Server 2017, the below BACKUP command is failing and SSMS shows up 2 error messages - Error 3202 & 3013.
BACKUP DATABASE [Promodag] TO DISK = N'\\ServerX\SQL\DBA_Full.bak' WITH INIT, CHECKSUM, COMPRESSION,STATS=10
Error:
Msg 3202, Level 16, State 1, Line 1
Write on "\\ServerX\SQL\DBA_Full.bak" failed: 112(There is not enough space on the disk.)
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.
But when executed in TRY CATCH, to capture the error, the ERROR_MESSAGE() has only the last error in it.
BEGIN TRY
BACKUP DATABASE [Promodag] TO DISK = N'\\ServerX\Backup\DBA_Full.Bak' WITH INIT, CHECKSUM, COMPRESSION,STATS=10
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER(),ERROR_MESSAGE() AS ErrorMessage
END CATCH
Output as table:
(No column name) ErrorMessage
3013 BACKUP DATABASE is terminating abnormally.
So, please help on how to concatenate all the error messages in the CATCH block.
I have one SQL Server instance accessed by many different applications.
Sometimes happened that one of such application throw the exception : "String or binary data would be truncated".
My objective is to trace (logging) when that error happen, and trace down which application has encountered the problem on which field.
I have no access to every application's code, so my first idea is to develop a solution directly in the SQL Server, but i don't know how can i check if that problem is occured and on which field.
but i don't know how can i check if that problem is occured and on which field.
even SQL server won't tell you on which field it occurred.There is a connect item ,which has been logged for the same
https://connect.microsoft.com/SQLServer/feedback/details/339410/please-fix-the-string-or-binary-data-would-be-truncated-message-to-give-the-column-name
But you can catch those errors,with a simple try catch and log them
create table #t1
(
charcol char(1)
)
begin try
insert into #t1
values
('a'),
('aa')
end try
begin catch
select error_message()
end catch
This is fixed in Recent versions of SQLServer..Now you will be able to know the exact column
Msg 2628, Level 16, State 1, Line 9
String or binary data would be truncated in
table 'StackOverflow2013.dbo.CoolPeople', column 'PrimaryCar'.
Truncated value: '2006 Subaru Impreza '.
This works in SQL Server 2019 if you enable database scoped settings like below
ALTER DATABASE SCOPED CONFIGURATION
SET VERBOSE_TRUNCATION_WARNINGS = ON;
you have to turn traceflag 460 for SQL Server 2016-2017
References and Examples:
https://www.brentozar.com/archive/2019/03/how-to-fix-the-error-string-or-binary-data-would-be-truncated/
I am having problems setting up change data capture on a SQL Server 2012 instance. Whenever I attempt to enable CDC on a table I get the following error:
Msg 22832, Level 16, State 1, Procedure sp_cdc_enable_table_internal,
Line 623
Could not update the metadata that indicates table
[dbo].[TableName] is enabled for Change Data Capture.
The failure
occurred when executing the command '[sys].[sp_cdc_add_job] #job_type
= N'capture''.
The error returned was 22836: 'Could not update the metadata for database [database name] to indicate that a Change Data Capture
job has been added. The failure occurred when executing the command
'sp_add_jobstep_internal'.
The error returned was 14234: 'The
specified '#server' is invalid (valid values are returned by
sp_helpserver).'. Use the action and error to determine the cause of
the failure and resubmit the request.'. Use the action and error to
determine the cause of the failure and resubmit the request.
The name of the server has not changed, I tried the sp_dropserver / sp_addserver solution and receive the following error:
Msg 15015, Level 16, State 1, Procedure sp_dropserver, Line 42
The server 'ServerName' does not exist. Use sp_helpserver to show
available servers.
Msg 15028, Level 16, State 1, Procedure sp_addserver, Line 74
The server 'ServerName' already exists.
As I've stated, I'm trying to set up CDC and not replication. The version of SQL Server is: 11.0.5058.0 (SQL Server 2012 SP2)
I've looked at Error while enabling CDC on table level and tried that solution.
I've also tried:
exec sys.sp_cdc_add_job #job_type = N'capture'
I receive the following error:
Msg 22836, Level 16, State 1, Procedure sp_cdc_add_job_internal, Line 282
Could not update the metadata for database [DatabaseName] to indicate
that a Change Data Capture job has been added. The failure occurred
when executing the command 'sp_add_jobstep_internal'.
The error returned was 14234: 'The specified '#server' is invalid (valid values are returned by sp_helpserver).'. Use the action and error to
determine the cause of the failure and resubmit the request.
Any help would be greatly appreciated.
As listed here, check the names match
SELECT srvname AS OldName FROM master.dbo.sysservers
SELECT SERVERPROPERTY('ServerName') AS NewName
If not, fix with:
sp_dropserver '<oldname>';
GO
sp_addserver '<newname>', local;
GO
The error is caused due to mismatch in value between SERVERPROPERTY(‘ServerName’)) and master.dbo.sysservers
Check these SQLs:
SELECT * FROM master.dbo.sysservers
SELECT SERVERPROPERTY('ServerName')
If your SERVERPROPERTY('ServerName') is not any of the sysservers, then the fix is to change your computer name to match one of those.
Adding the server fixes the issue:
DECLARE #ServerName NVARCHAR(128) = CONVERT(sysname, SERVERPROPERTY('servername'));
EXEC sp_addserver #ServerName, 'local';
GO
I had a similar situation, where I have restored a bak file which I got from another windows machine,which retained windows user account with the old PC name. I had delete the backup, create an empty data base and restore into that. This solved my issue
select log_reuse_wait_desc from sys.databases where name = 'mydb'
1. LOG_BACKUP
All update and insert query throws :
ODBC Error: ODBC RC=-1, ODBC
SQLState=37000, DBMS RC=9002, DBMS Msg=[Microsoft][ODBC SQL Server
Driver][SQL Server]The transaction log for database 'mydb' is full. To
find out why space in the log cannot be reused, see the
log_reuse_wait_desc column in sys.databases. Operation canceled
My query:
First I delete and insert data into STATUS TABLE:
String insertQuery = "insert into "+dbmsName+"."+schemaName+".status(siteId,Severity) values(?,?)";
String deleteQuery = "delete from "+dbmsName+"."+schemaName+".status";
Now I select from status table and update live table:
String updateQuery = "update "+dbmsName+"."+schemaName+".live set status = ? where new_site_id = ?";
String updateAllQuery = "update "+dbmsName+"."+schemaName+".live set status = site_status where new_site_id = ?";
Now I can't even use any other update queries too.
How can I solve this issue?
"The transaction log for database 'mydb' is full" - that's the problem.
You need to free up disk space. Until you do that, you won't ba able to do much.
Do you have a regular T-LOG maintenance schedule? If you are in FULL recovery mode and have no backups running, then the transaction will simply continue growing.
To shrink the transaction log for your database (Don't do this normally, just when encountering your current situation):
8 Steps to better Transaction Log throughput
Has a maximum size been set for your database? Run this to find out:
sp_helpdb mydb
go
Update: You should perform a transaction log back up. You probably have to back it up more than once. After you back up the transaction log, try shrinking it.
Factors That Can Delay Log Truncation
I'm trying to rename a table using the following syntax
sp_rename [oldname],[newname]
but any time I run this, I get the following [using Aqua Datastudio]:
Command was executed successfully
Warnings: --->
W (1): The SQL Server is terminating this process.
<---
[Executed: 16/08/10 11:11:10 AM] [Execution: 359ms]
Then the connection is dropped (can't do anything else in the current query analyser (unique spid for each window))
Do I need to be using master when I run these commands, or am I doing something else wrong?
You shouldn't be getting the behaviour you're seeing.
It should either raise an error (e.g. If you don't have permission) or work successfully.
I suspect something is going wrong under the covers.
Have you checked the errorlog for the ASE server? Typically these sorts of problems (connections being forcibly closed) will be accompanied by an entry in the errorlog with a little bit more information.
The error log will be on the host that runs the ASE server, and will probably be in the same location that ASE is installed into. Something like
/opt/sybase/ASE-12_5/install/errorlog_MYSERVER
try to avoid using "sp_rename". Because some references in system tables remain like old name. Someday this may cause some faulties if you forget this change.
I suggest;
select * into table_backup from [tableRecent]
go
select * into [tableNew] from table_backup
go
drop table [tableRecent] -- in case of backup you may not drop that table
go
drop table table_backup -- in case of backup you may not drop that table
go
to achieve that; your database has an option "select into/bulkcopy/pllsort"
if your ata is huge, check your free space on that database.
and enjoy :)