Error while enabling CDC on table level - sql

I am enabling Change data capture (CDC) on SQL server 2012 Enterprise edition(11.0.2100.60)
. I am able to enable it on Database level with below SQL, but failed to enable on Table level.
Use DatabaseName
GO
Exec sys.sp_cdc_enable_db
GO
EXEC sys.sp_cdc_enable_table #source_schema = N'dbo'
,#source_name = N'TableName'
, #role_name = NULL
GO
Got Error like,
'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 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.'. Use the action and error to
determine the cause of the failure and resubmit the request.'
Would anyone help me to out of this?
Thanks in advance..!!

I had the same problem recently.
In my opinion, it is highly probable that the name of the computer has changed.
If it is true, read on ...
Name Changing the computer is automatically recognized by SQL. However, system metadata must be updated manually.
(Source: http://msdn.microsoft.com/en-us/library/ms143799.aspx)
SQL Statement
sp_dropserver old_name\instancename;
GO
sp_addserver new_name\instancename, local;
GO
Example
sp_dropserver name-PC\SQLEXPRESS;
GO
sp_addserver CLT55\SQLEXPRESS, local;
GO
Reference problem and solution

Related

Execute SQL Server stored procedure through SSIS

I have this stored procedure:
CREATE PROCEDURE [dbo].[sp_Carrier_Scan_Compliance]
(#RETAILERID INT OUTPUT,
#SYSTEM_ID VARCHAR(10) OUTPUT)
AS
BEGIN
SET #RETAILERID = 2
SET #SYSTEM_ID = 'DMASOS'
...
END
I have created a SSIS package using a Execute SQL Task in the control flow.
These are my Execute SQL Task editor settings:
This are my Variable settings:
These are my Parameter Mapping settings:
When I run the SSIS package, I get an error:
Error: 0xC002F210 at Execute SQL Stored Procedure (to copy data from 'BI-Datatrunk' source table) Task, Execute SQL Task: Executing the query "exec = [sp_Carrier_Scan_Compliance] ? OUTPUT, ? O..." failed with the following error: "Incorrect syntax near '='.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task failed: Execute SQL Stored Procedure (to copy data from 'BI-Datatrunk' source table) Task
Warning: 0x80019002 at Carrier_Scan_Compliance_SP: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
I am not sure what I am missing.
Please help me.
Thanks
The key part of the last error is
The EXECUTE permission was denied on the object
'sp_Carrier_Scan_Compliance', database 'DATAK', schema 'dbo'."
You need to assign EXECUTE permissions to the SQL user executing the Proc
USE DATAK
GO
GRANT EXECUTE ON sp_Carrier_Scan_Compliance TO <sql user>
GO

Database Recovery Model alter script error

I am trying to deploy one of our server via teamcity (db project in VS 2012), i am getting an error on Staging server
Error SQL72014: .Net SqlClient Data Provider:
Msg 5069, Level 16, State 1, Line 5
ALTER DATABASE statement failed.
Error SQL72045: Script execution error.
The executed script:
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET RECOVERY SIMPLE
WITH ROLLBACK IMMEDIATE;
END
Any ideas are appreciated.
Don't just gloss over the problem: understand it.
It's because you have some sort of HA (you said mirroring in the comment above, but availability groups (for sure) or log shipping (I think) would also cause this) that requires that the database be in full recovery. When you try to set it to simple recovery, it fails because you can't set the recovery model to anything but full for such a database. You could guard against this by checking what environment you're deploying to from VS and act accordingly.

Text output from a stored proc

I have created a SQL stored procedure that produces the expected results when I call it from SQL Management Studio, it has one output parameter which is a message that tells me where the proc failed, if it fails. Here is the top part of the proc
CREATE PROCEDURE [dbo].[uspProgramUpdate]
#EmailFailMessage VARCHAR(100) OUTPUT
At the bottom of my stored procedure is where I set the value of the variable:
BEGIN CATCH
ROLLBACK TRAN [CoS1]
RAISERROR ('***An error occurred during processing, all transactions have been rolled back. Correct the error and restart this process.***', 16, 1)
SET #EmailFailMessage = '***An error occurred during processing, all transactions have been rolled back. Correct the error and restart this process.***'
END CATCH
What I am trying to do (that I can't figure out) is how to capture the text of EmailFailMessage so I can use that text in an email within SSIS.
I have created an Execute SQL Task in SSIS which executes the stored procedure successfully on the SQL server. I have successfully captured the RAISERROR event and I can see that in the output window within Visual Studio window when I run the SSIS package:
Error: 0xC002F210 at Execute SQL - run uspProgramUpdate, Execute SQL Task: Executing the query "EXEC uspProgramUpdate ?" failed with the following error: "***An error occurred during processing, all transactions have been rolled back. Correct the error and restart this process.***". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task failed: Execute SQL - run uspProgramUpdate
The SQLStatement property of the SSIS task is:
EXEC uspCoSSleepProgramUpdate ? OUTPUT
I have the following SSIS variable:
User::EmailFailMessage (string), value = asdf
and the parameter is mapped on the SSIS SQL task in Parameter Mappings as follows:
Variable Name| Direction | Data Type | Parameter Name | Parameter Size
User::EmailFailMessage | Output | VARCHAR | #EmailFailMessage | -1
I have an email task that is being called and for that email task I have set:
MessageSource = #[User::EmailFailMessage]
When I run the SSIS package and receive the email, the message (body) of the email doesn't have the message from SQL after running the stored proc. It does have the initial value set for the User::EmailFailMessage variable and I am expecting that to change, but it isn't.
I have also tried, among other things, using 0 for the parameter name in the Parameter Mapping area of my Execute SQL Task. Using that doesn't change the result.
Personally I use the error message variable that is available in the on Error event handler called User::dataFlowExceptionMessage. That will send the exact error message sent by the failing task. That would be what bubbled up from raiserror. BUt I would suggest that you use the real error message as part of your raise error mesaage to make it easier to daignose the reason why the rollback occurred. I want to know the exact error generally.
Another thing you can do is put your error into a table variable and then insert it into an exception table with the date and the proc name after the rollback. Table variables stay in scope thorugh a rollback. Then you could look in that table to pull the message for your email task and you also have a record of just exactly what failed and when and how often which is useful for troubleshooting.

SQL Server error "Could not continue scan with NOLOCK due to data movement."

I am having an issue when running queries or stored procedures. Every time I run a query I get the following error:
Could not continue scan with NOLOCK due to data movement.
If I remove the WITH NOLOCK command, I get a different error:
Msg 824, Level 24, State 2, Line 1
SQL Server detected a logical consistency-based I/O error: incorrect pageid (expected 1:19818941; actual 1:19818957). It occurred during a read of page (1:19818941) in database ID 9 at offset 0x000025cd37a000 in file 'E:\SQLDATA\MSCRM.mdf'. Additional messages in the SQL Server error log or system event log may provide more detail. This is a severe error condition that threatens database integrity and must be corrected immediately. Complete a full database consistency check (DBCC CHECKDB). This error can be caused by many factors; for more information, see SQL Server Books Online.
What should I do to resolve this error?
First, obviously, try DBCC CHECKDB.
If that cannot resolve the issue, you may need to restore from a backup and then manually copy over the most recent changes. Hopefully you have been doing nightly backups... ?
If the error is prefixed with any object (Proc, trigger, function), then you can just drop and create the object again or alter it if possible.

ALTER DATABASE failed because a lock could not be placed on database

I need to restart a database because some processes are not working. My plan is to take it offline and back online again.
I am trying to do this in Sql Server Management Studio 2008:
use master;
go
alter database qcvalues
set single_user
with rollback immediate;
alter database qcvalues
set multi_user;
go
I am getting these errors:
Msg 5061, Level 16, State 1, Line 1
ALTER DATABASE failed because a lock could not be placed on database 'qcvalues'. Try again later.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.
Msg 5061, Level 16, State 1, Line 4
ALTER DATABASE failed because a lock could not be placed on database 'qcvalues'. Try again later.
Msg 5069, Level 16, State 1, Line 4
ALTER DATABASE statement failed.
What am I doing wrong?
After you get the error, run
EXEC sp_who2
Look for the database in the list. It's possible that a connection was not terminated. If you find any connections to the database, run
KILL <SPID>
where <SPID> is the SPID for the sessions that are connected to the database.
Try your script after all connections to the database are removed.
Unfortunately, I don't have a reason why you're seeing the problem, but here is a link that shows that the problem has occurred elsewhere.
http://www.geakeit.co.uk/2010/12/11/sql-take-offline-fails-alter-database-failed-because-a-lock-could-not-error-5061/
I managed to reproduce this error by doing the following.
Connection 1 (leave running for a couple of minutes)
CREATE DATABASE TESTING123
GO
USE TESTING123;
SELECT NEWID() AS X INTO FOO
FROM sys.objects s1,sys.objects s2,sys.objects s3,sys.objects s4 ,sys.objects s5 ,sys.objects s6
Connections 2 and 3
set lock_timeout 5;
ALTER DATABASE TESTING123 SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Try this if it is "in transition" ...
http://learnmysql.blogspot.com/2012/05/database-is-in-transition-try-statement.html
USE master
GO
ALTER DATABASE <db_name>
SET OFFLINE WITH ROLLBACK IMMEDIATE
...
...
ALTER DATABASE <db_name> SET ONLINE
Just to add my two cents. I've put myself into the same situation, while searching the minimum required privileges of a db login to run successfully the statement:
ALTER DATABASE ... SET SINGLE_USER WITH ROLLBACK IMMEDIATE
It seems that the ALTER statement completes successfully, when executed with a sysadmin login, but it requires the connections cleanup part, when executed under a login which has "only" limited permissions like:
ALTER ANY DATABASE
P.S. I've spent hours trying to figure out why the "ALTER DATABASE.." does not work when executed under a login that has dbcreator role + ALTER ANY DATABASE privileges. Here's my MSDN thread!
I will add this here in case someone will be as lucky as me.
When reviewing the sp_who2 list of processes note the processes that run not only for the effected database but also for master. In my case the issue that was blocking the database was related to a stored procedure that started a xp_cmdshell.
Check if you have any processes in KILL/RollBack state for master database
SELECT *
FROM sys.sysprocesses
WHERE cmd = 'KILLED/ROLLBACK'
If you have the same issue, just the KILL command will probably not help.
You can restarted the SQL server, or better way is to find the cmd.exe under windows processes on SQL server OS and kill it.
In SQL Management Studio, go to Security -> Logins and double click your Login. Choose Server Roles from the left column, and verify that sysadmin is checked.
In my case, I was logged in on an account without that privilege.
HTH!
Killing the process ID worked nicely for me.
When running "EXEC sp_who2" Command over a new query window... and filter the results for the "busy" database , Killing the processes with "KILL " command managed to do the trick. After that all worked again.
I know this is an old post but I recently ran into a very similar problem. Unfortunately I wasn't able to use any of the alter database commands because an exclusive lock couldn't be placed. But I was never able to find an open connection to the db. I eventually had to forcefully delete the health state of the database to force it into a restoring state instead of in recovery.
In rare cases (e.g., after a heavy transaction is commited) a running CHECKPOINT system process holding a FILE lock on the database file prevents transition to MULTI_USER mode.
In my scenario, there was no process blocking the database under sp_who2. However, we discovered because the database is much larger than our other databases that pending processes were still running which is why the database under the availability group still displayed as red/offline after we tried to 'resume data'by right clicking the paused database.
To check if you still have processes running just execute this command:
select percent complete from sys.dm_exec_requests
where percent_complete > 0