Having a database with a main table called "MasterTable". Found out that there are some columns in MasterTable that are no longer reasonable (from business purpose). To make everything slimer and easier I'd like to delete those columns.
In case I delete column ABC a view that refers to that column or a stored procedure doing an insert/update might fail when ABC is missing.
For this I'd like to query my database to see all views/procedures that somehow use ABC, before deleting it.
Something like this should help you . But please remember:
It will also give you results where the column might be commented out..
If you use select * in view or procedure it will not be shown in the result
SELECT OBJECT_NAME(OBJECT_ID),definition
FROM sys.sql_modules
WHERE definition LIKE '%' + 'YourColumnName' + '%'
Related
I have a table which has 50+ columns but only few columns are getting used. that means when any stored procedure uses that table it only refers 4-5 columns in select/where statements . rest of columns are not getting used . i just want to list down those columns that are actually getting used. one way is finding out the dependencies of a table and then go through every SP and find out which columns are getting used . but in that case i have around 30+ Sp. is there any efficient way to do it.
To use multiple columns in a procedure, you can use a code like below
create procedure sp_sample
#column_names varchar(200)
as
if #column_names='' or #column_nams is null
set #column_names='*'
exec ('select '+#column_name +' from table')
Here are some examples :
exec sp_sample #columnname='id,name'
or
exec sp_sample #columnname='id,name,telphone'
Try this:
select name from syscomments c
join sysobjects o on c.id = o.id
where TEXT like '%table_name%' and TEXT like '%column_name%'
In table_name give you table name, in column_name give the column for which you want to chck the procedure dependencies.You will get the stored procedure names as output
If you import your database as a database project using the SQL Server Data Tools, you will be able to find all references to a table or column using the "Find All References" context command. What makes this particularly useful is the accuracy: it will even find instances of SELECT * that don't mention the column explicitly, but implicitly refer to it anyway. It will also not be confused by tables or columns with similar names (finding particular instances of ID is otherwise rather problematic).
If all you want to know if a column is referenced at all, you can simply delete it and see if any "unresolved reference" errors appear in the error list -- if yes, then the column is used somewhere.
In Microsoft SQL Server 2008 R2, let's say my database has the following view:
create view [dbo].[MyView]
(
[MyColumnA]
)
AS
(SELECT MyColumnB FROM MyTable)
Now let's suppose I only know that there is a view called MyView that has a column called MyColumnA, but I don't know that it maps to MyTable.ColumnB. What is the easiest/fastest way to determine which table and column MyView.ColumnA maps to? Is there a query that can tell me this? Something like:
SELECT TABLE_NAME, TABLE_COLUMN_NAME
FROM INFORMATION_SCHEMA.VIEW_MAPPINGS
WHERE VIEW_NAME = 'MyView' AND VIEW_COLUMN_NAME = 'MyColumnA'
This query would return [MyTable, MyColumnB].
Currently I have to find the view in SSMS Object Explorer, right click it and generate the create script, then search for the name of the view's column. Then I note which ordinal position it is in the view (let's say 4th column), and have to find the corresponding 4th column in the select statement. The select statement will most likely be using a table alias, so then I have to look through the JOIN statements to find the table name based on the alias.
This is quite time consuming, and I'm hoping to find a faster way, if not by a query then perhaps by some other process that is faster or easier than mine.
SP_DEPENDS should work
SP_DEPENDS 'MyView'
Good day, I am specifically trying to perform a search through my storedprocedures with the aim of finding a procedure that does not contain a cetain word. Example "nolock".
I understand and i successfully achieved this with a query like below.
Select Name as [Procedurees with no nolock] from
database.sys.procedure WHERE (OBJECT_DEFINITION(OBJECT_ID)
NOT LIKE '%myKeyword%' .
The above successfully returned the names of procedures without the keyword in them. However, the stored procedures script are mostly long and may require the use of the keyword more than one time in a single procedure. Example may be after each "FROM TABLE name" i need a key word "with nolock" . I want the search to be perform till the end of the procedure and where exist a "TABLE NAME", a "mykeyword" must follow else return the procedure name among those with no "mykeyword".
Since i have like twelve table names i want to use, i thought i could use CASE like
Select Name as [Procedurees with no nolock] from database.sys.procedure
WHERE (OBJECT_DEFINITION(OBJECT_ID) NOT LIKE '%Mytable NAME , then plus the keyword%'.
This does not look ok for me because i have quite few tables names and it also possible that the table name may appear numerous time in the procedure and may be have the 'keyword' for the first time only. Please is there anyway i can achieve this ? Also sorry for my inability to explain clearly. Any help would be appreciated
Sorry, but your question doesn't really make any sense to me. But I'll try to answer.
Blockquote
because i have quite few tables names and it also possible that the table name may appear numerous time in the procedure
Can't you just use a GROUP BY to restrict the results to one set per table?
Select Name as [Procedurees with no nolock] from database.sys.procedure
WHERE (OBJECT_DEFINITION(OBJECT_ID) NOT LIKE '%Mytable NAME , then plus the keyword%'
GROUP BY database.sys.procedure
If you want to do this for each table in your database, you can use this:
DECLARE #keyword NVARCHAR(20) = 'words'
sp_MSForEachTable 'Select Name as [Procedurees with no nolock] from database.sys.procedure
WHERE (OBJECT_DEFINITION(OBJECT_ID) NOT LIKE ? AND , (OBJECT_DEFINITION(OBJECT_ID) LIKE ''' + #KeyWord + '''
GROUP BY database.sys.procedure'
The ? is replaced by the table name in the stored procedure.
Why don't you turn it around...?
Find the procedures that DO match, and then exclude them ?
select name from sys.procedures
where name not in (select name from sys.procedures WHERE (OBJECT_DEFINITION(OBJECT_ID) LIKE '%blah%'))
I'm new to SQL Server programming, so this may or may not be a stupid question.
So, first I do not know what my input table is (my task is supposed to work with ANY table).
Second, I get the column names using sp_help and then I select only that column into another table. Now I need the rows from COLUMN_NAME to be the names of god knows how many columns into some new table.
I tried something using PIVOT, but I can't seem to make it work.
It sounds like you want to access the metadata tables. Something like this may put you in the right direction:
insert into column_name(name)
select column_name
from information_schema.columns
where table_name = XXX <----- YOUR TABLE GOES HERE
I'm always being given a large list of say id's which I need to search in our database have manually put them into a sql statement like the follow which can take a while putting single quotes around each number followed by a comma, I was hoping someone has a easy way of doing this for me? Or am I just being a bit lazy...
select * from blah where idblah in ('1234-A', '1235-A', '1236-A' ................)
You can use the worlds' simplest code generator.
Just paste in the list of values, setup the pattern and voila... you have a set of quoted values.
I have also used Excel in the past, using the CONCAT function with smart paste.
I would set aside a table to hold the values and have my queries JOIN against that table. Set up a simple import script (don't forget to clear out the table at the start) and something like this is a breeze. Run the import, run the query. You never have to touch the query again or regenerate any code.
As an example:
CREATE TABLE Search_ID_List (
id VARCHAR(20) NOT NULL,
CONSTRAINT PK_Search_ID_List PRIMARY KEY CLUSTERED (id)
)
and:
SELECT
<column list>
FROM
Search_ID_List SIL
INNER JOIN Blah B ON
B.id = SIL.id
If you want to be able to save past search criteria or have multiple searches available to you at the same time then you can just add an identifying column which gets filled in by your import. It can be the file from where the ids came, some descriptive code/name, or whatever. Then just add that to the WHERE clause of your query and you're all set.
You could do something like this.
select * from blah where ',' + '1234-A,1235-A,1236-A' + ',' LIKE ',%' + idblah + '%,'
This pattern is super useful when you're being passed a comma delimited list of values to filter by, but I think would be applicable here as well.