I am using SQL Server 2008 through SQL Server Management Studio
I have never seen this before:
Often, when refactoring a database as I have been doing most of my day, I use the following to find references to columns, tables and other procedures in my existing procedures:
select *
from sys.sql_modules
where [definition] like '%SomethingImLookingFor%'
This is nothing special, I'm sure lots of database heads have done it before.
However, I noticed the results contained a procedure that looks to have been a total accident when created(If ever actually created). In the database I have a procedure named ChatObject_Remove, this appeared in my query because I was looking for anything referencing the term %Remove%. But I also see a ghost procedure called zzChatObject_Remove
In SSMS when I expand the database's Programmability -> Stored Procedures I see ChatObject_Remove, but not zzChatObject_Remove.
I have tried refreshing this group and the ghost still does not appear.
I tried to use the statement
drop procedure [dbo].[zzChatObject_Remove]
which returned an error saying it either didn't exist or I didn't have permission.
I tried to create a procedure called zzChatObject_Remove and it allowed me to do it (but now I had 2 objects in sql_modules with a definition for create procedure zzChatObject_Remove ...). I then deleted the new one which it allowed, and the ghost, as expected, is still in the modules table and I cannot touch it.
Has anyone seen this before? Where else could this be? Could it do any harm? How can I get rid of this seemingly orphaned procedure in the sys.sql_modules table?
Thanks in advance for any input.
Just my luck, seeing a ghost around Halloween time...
Try this to see where it comes up:
exec sp_MSforeachdb '
select ''?'' [db]
, s.name [schema]
, o.name [object]
, o.type_desc
from [?].sys.objects o
inner join sys.schemas s
on s.schema_id = o.schema_id
where o.name = ''zzChatObject_Remove''
'
Or the following:
select *
from sys.sql_modules sm
inner join sys.objects o
on o.object_id = sm.object_id
inner join sys.schemas s
on s.schema_id = o.schema_id
where sm.description like '%zzChatObject_Remove%'
Also, to check, is this proc defined in sql_modules, or is it showing because it's referenced in the code of another procedure?
Related
I use Microsoft SQL server. I have a db with hundreds of Stored Procedures. To find the dependencies, I can right click the SP, select view dependencies, and click objects that depend on [name].
I have this query to find all the stored procs
select * from INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE = 'procedure'
and if I add this, I exclude every single system SP.
and LEFT(routine_name, 3) not in ('sp_','xp_','ms_')`
I would like to find ALL the SPs that no other proc/function/trigger depends on.
Ex: I have sproc1. In the right click + view dependencies I have at objects that depend on [sproc1] no elements, meaning that nothing depends on this SP.
Using a query to find procs that were unaltered for a year or more is not an option since some operations execute every leap year. Is there a way to write a query to determine the name of the stored proc and schema_id only for the stored procs that NOONE depend on? (having zero objects that depend on [this_sp] )
You can modify list of searched objects below:
SELECT *
FROM sys.objects o
WHERE NOT EXISTS
(
SELECT *
FROM sys.sql_expression_dependencies e
WHERE e.referenced_id = o.object_id
)
AND o.type IN (N'V', N'P', N'TF', N'IF', N'FN')
I tried to alter a table with a new table name. I successfully changed the name but the schema also changed from "dbo." to be "dbo.dbo.". When I tried selecting data from the new table, it shows in the Message box that the table is invalid so I can't do anything with the new table.
Does anyone know how can I change the table back to the original schema? I used WINSQL with sql server 2008. Thanks,
My guess is that you've actually simply renamed the table to [dbo.tablename] and its fully qualified name is [dbname].[dbo].[dbo.tablename]. This happens when you right-click to rename a table name in SSMS and I'd imagine that WinSQL is doing the same thing (though I don't know why you're using that tool when SSMS is included). When you right-click, it takes away the schema name which makes you believe you need to fully qualify the new name, but you don't.
You should be safe to right-click and rename the table name to just the new table name.
To be sure, though, you can run:
select *
from sys.schemas
where name = 'dbo.dbo';
just to confirm that you've not created a new schema.
EDIT
Just for the sake of completeness I'll incorporate the comment made by #billinkc:
Run this query to get the exact schema of the table:
select
s.name as SchemaName,
t.name as TableName
from sys.schemas s
join sys.tables t
on s.schema_id = t.schema_id
where t.name = 'tablename'
On occasion, I'm interested in getting a list of columns in one of the tables or views in my SQL Server 2008 R2 database. It's useful, for example, if you're building database documentation without using an expensive off-the-shelf product.
What's an easy way to get this information?
In SQL Server 2008 R2 (among other versions), there are system views provided automatically with every database. As long as you are connected to the database where your table resides, you can run a query like this:
DECLARE #TableViewName NVARCHAR(128)
SET #TableViewName=N'MyTableName'
SELECT b.name AS ColumnName, c.name AS DataType,
b.max_length AS Length, c.Precision, c.Scale, d.value AS Description
FROM sys.all_objects a
INNER JOIN sys.all_columns b
ON a.object_id=b.object_id
INNER JOIN sys.types c
ON b.user_type_id=c.user_type_id
LEFT JOIN sys.extended_properties d
ON a.object_id=d.major_id AND b.column_id=d.minor_id AND d.name='MS_Description'
WHERE a.Name=#TableViewName
AND a.type IN ('U','V')
Of course, this is just a starting point. There are many other system views and columns available in every database. You can find them through SQL Server Management Studio under Views > "System Views
Another way is querying the INFORMATION_SCHEMA.columns view as detailed here:
Information_Schema - COLUMNS
This will give you information for all the columns in the current database (and what table/view they belong to) including their datatypes, precision, collation and whether they allow nulls etc
Usefully as well, these views are maintained in multiple DBMS programs too, so you could potentially use the same or similar query to get the same information regarding a MySQL database as you can a SQL Server DB, which could be useful if you are developing on multiple platorms.
sp_columns returns detailed information about each of the columns in the table. SO Answer
sp_columns #tablename
sp_help returns detailed information about the entire table including the columns and constraints. SO Answer
sp_help #tablename
To get a list of Columns of a view with some other information about the column you can use the following:
SELECT * FROM sys.columns c, sys.views v
WHERE c.object_id = v.object_id
AND v.name = 'view_Name'
GO
And if you only want the list of Column Name use this.
SELECT c.name
FROM sys.columns c, sys.views v
WHERE c.object_id = v.object_id
AND v.name = 'view_UserAssessphers'
GO
exec sp_helptext <your view name>
Also works for the view only, blachniet's answer is best if you need details on the columns in the table.
In a new query window, type the name of the view/table, highlight it, and press Alt-F1. This will run sp_help, like blachniet suggested.
simple list of column names without any further information.
SELECT COLUMN_NAME FROM TABLENAME.INFORMATION_SCHEMA.COLUMNS;
Replace TABLENAME with your tables name.
i have used SQL server 2012 and made a huge database with many relations. today i realized using "NCHAR" instead on "NVARCHAR" adds space on end of strings which i don't like it. is there any way to recreate database with relations. I've tried "drop and create" for tables but needs to remove all relations and define them again which is very time consuming.
It's possible to alter only the affected tables? like this:
SELECT 'alter table '+s.name+'.'+t.name+
' alter column '+c.name+' nvarchar('+convert(varchar(11),c.max_length/2)+') go'
FROM sys.tables as T
inner join sys.schemas as s
on T.[schema_id]=s.[schema_id]
inner join sys.columns as C
on T.[object_id]=C.[object_id]
and c.system_type_id=239--nchar type
, and before that don't forget to use the same kind of mechanism to generate an update statement to remove extra spaces.
I have created a new database with copies of existing tables but changed the names of these tables, is there a SQL script that I can run (maybe using SysObjects) to change all references to these tables in all stored procedures?
DO NOT RELY ON INFORMATION_SCHEMA.ROUTINES because ROUTINE_DEFINITION is only nvarchar(4000). You need to sys.sql_modules where definition is nvarchar(max)
try any of these to find the procedure that you need to modify:
SELECT DISTINCT
LEFT(s.name+'.'+o.name, 100) AS Object_Name,o.type_desc --, m.definition
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
INNER JOIN sys.schemas s ON o.schema_id=s.schema_id
WHERE m.definition Like '%'+#SearchValue+'%'
ORDER BY 1
SELECT
OBJECT_SCHEMA_NAME(m.object_id)+'.'+OBJECT_NAME(m.object_id) --, m.definition
FROM sys.sql_modules m
WHERE m.definition like '%whatever%'
SELECT
OBJECT_SCHEMA_NAME(m.object_id)+'.'+OBJECT_NAME(m.object_id), o.type_desc
--,m.definition
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
WHERE m.definition like '%whatever%'
you can uncomment m.definition to list out the content, but I find it better to just ID all the procedures and then review them manually, because you don't want to run UPDATE commands on the system tables. Script out the necessary procedures, make the changes (search/replace or manually), and then run the scripts!!!
No.
I believe SQL Refactor from Redgate has this functionality. Otherwise you could script out all objects and either manually or via code do a search and replace.
SQL Server 2005 also has support for synonyms that might be of some help.
Here is a chunk of SQL that you could use to retrieve the definition of stored procedures that match a certain search criteria. You could simply change it to do a search and replace like Martin had suggested.
Simply change '%TABLE_NAME%' to your search criteria or the table name you would like to change.
SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%TABLE_NAME%' AND ROUTINE_TYPE='PROCEDURE'
If it's short term (like a testing database), the easier solution may be to make a VIEW for each table that you changed, using the old name. For instance, if you changed the table tests to tests_new you can do:
CREATE VIEW dbo.tests
AS
SELECT * FROM dbo.tests_new
All your procedures will that reference dbo.tests will actually look at the data in dbo.tests_new.
This is a very very bad idea if this will be a permanent/production DB, as it just adds a layer of obfuscation to your structure and will make it a nightmare to maintain.
You can't change the table references in sprocs through the system data dictionary - you will have to get the script that creates the stored procedure and change the table names in the script. If you have the scripts this is a simple search and replace for the most part.
If you don't have the scripts you can get the text of the stored procedure scripts from sys.sql_modules, or retrieve it through SSMS.