I am trying to run the following:
EXEC sp_rename '"database_name"."table_name"."old_column_name"', 'new_column_name';
But I am getting an error:
Msg 15225, Level 11, State 1, Procedure sp_rename, Line 374
No item by the name of '"database_name"."table_name"."old_column_name"' could be found in the current database 'master', given that #itemtype was input as '(null)'.
It can't properly find the column you're trying to change.
USE database_name
GO
EXEC sp_rename 'table_name.old_column_name','new_column_name','COLUMN'
GO
It works without the COLUMN object type, but added for security purposes.
Look at Microsoft's website for more info.
Related
I get an error from one of my databases when trying to execute this one
create or alter procedure [dbo].[test_sp]
with execute as owner
as
SELECT SUSER_SNAME()+ ' '+ USER_NAME();
begin
exec master..sp_trace_generateevent #eventid = 82 ,
#userinfo=N'test'
end
GO
exec [dbo].[test_sp]
Error:
Msg 8189, Level 14, State 10, Procedure master..sp_trace_generateevent, Line 1 [Batch Start Line 9]
You do not have permission to run 'SP_TRACE_GENERATEEVENT'.
Granted ALTER TRACE to my user (which returns in SUSER_SNAME()), but it wasn't help
The same script on the second database (same server) works without errors.
What else can it be?
You're trying to run this with EXECUTE AS OWNER, and the owner is a database-level principal and you can't operate outside the current database while impersonating a database-level principal. Switch to EXECUTE AS CALLER (the default) to have the caller's identity used to run the proc in master. eg
create or alter procedure [dbo].[test_sp]
with execute as caller
as
SELECT SUSER_SNAME()+ ' '+ USER_NAME();
begin
exec master..sp_trace_generateevent #eventid = 82, #userinfo = N'test'
end
GO
exec [dbo].[test_sp]
This can be made to work with owner-impersonation by marking the database as TRUSTWORTHY. See: Extending Database Impersonation by Using EXECUTE AS and Guidelines for using the TRUSTWORTHY database setting in SQL Server
I created a database trigger last week in our QA environment to track schema and database changes. I am trying to remove the tables, and I have removed all the triggers but when I try to drop the last table I get the following error
Msg 208, Level 16, State 1, Procedure trg_Database_changes, Line 8 [Batch Start Line 2]
Invalid object name 'Audit_logs'
This makes me think I still have a trigger out there but I can't find it. I have looked in every database.
Any ideas?
USE [master]
GO
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Audit_logs]') AND type in (N'U'))
DROP TABLE master.[dbo].[Audit_logs]
GO
Is there any way to find the SQL query errors without executing the query in SQL server?
For example,
Use TestDB
Select * from Employee1
Consider TestDB doesn’t contain any table named “Employee1”. In this case, while we running this query, we will get the “Invalid object name Employee1” SQL error.
My question is that, after writing the query, is there any better approach to find these errors without executing?
I got a comment from my colleague is that use the sp_describe_first_result_set System stored procedure.
If you have 2012 and above you can use the sp that your colleague has mentioned.
If you use 2008 R2 and less then use
SET FMTONLY ON;
Select * from MyTabl;
SET FMTONLY OFF;
Msg 208, Level 16, State 1, Line 3
Invalid object name 'MyTabl'.
This will execute the query but not process any data. NOEXEC only parses the query so any objects that don't exist will not be flagged. NOEXEC checks syntax only. So what you want is either sp_describe_first_result_set or FMTONLY.
Alternatively if you have a select then you can also append to your where 1=2. For example
SELECT *
FROM MyTabl
WHERE 1=2
which gives them same result
Msg 208, Level 16, State 1, Line 3
Invalid object name 'MyTabl'
I tried using this procedure to rename badly named columns in SQL Server. The generated statement seems correct:
EXEC sp_rename '[TBL_TAXREPORTtestxxx].["InsertedOn"]', 'InsertedOn', 'COLUMN'
however that gives me the following error:
Msg 15248, Level 11, State 1, Procedure sp_rename, Line 266
Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong.
Any clue ?
Option 1: You can try to check if you are running the query in the correct database.
Option 2: If yes then try this:
EXEC sp_rename
#objname = 'TBL_TAXREPORTtestxxx."[InsertedOn]"', --or #objname = 'TBL_TAXREPORTtestxxx."InsertedOn"'
#newname = 'InsertedOn',
#objtype = 'COLUMN'
Option 3: If the above also fails then you can try to create a new table with all the names correct and copy the data from the existing table to the new table and drop the previous one. And finally rename the table.
EDIT:
Option 4: As Gordon has said in comments, you might also want to check for schema.
EXEC sp_rename
#objname = '[dbo].TBL_TAXREPORTtestxxx."[InsertedOn]"',
#newname = 'InsertedOn',
#objtype = 'COLUMN'
We are getting this error when trying to execute an ALTER TABLE command on an existing table:
Msg 208, Level 16, State 1, Procedure sp_MStran_altertable, Line 1071
Invalid object name 'systranschemas'.
The alter table statement is:
alter table dbo.YARPF
add YARSCWO char(3)
We have admin rights on the SQL server and database.
What can we do to track down and resolve this error?
The sp_MStran_altertable procedure is called whenever an object is altered in a replicated database. You are getting the error because replication was partially removed.
You need to manually remove all references to replication. Technet has a good article on how to do this: http://technet.microsoft.com/en-us/library/ms152757(v=sql.105).aspx