How to check, find and see the values of a specific column inside of a big database with many tables, if the column actually exists [duplicate] - sql

This question already has answers here:
Find a string by searching all tables in SQL Server
(12 answers)
Closed 1 year ago.
I have a lot of tables inside some database of SQL Server and I think that some of those tables have some specific column that, I want to check if it exists and also see the values inside, if it actually exists.
I guess that the column was named like: ‘’Status’’
Please, consider that I don't have any idea about what are the values that maybe exist inside of this supposed column or even the kind of it.
Database name: PrincipalGroup
I won't say the name of the tables, because I don’t think it's feasible to write all the tables in this query, because there are many.
So, the point is: how can I query this by the easiest and simplest way?

You can try the following query that will produce a list of SQL statements you can then execute to "see the values inside" each table that contains the column named Status, and obviously tweak to your specific requirements.
select Concat('select distinct ', QUOTENAME(table_name, ''''), ' , [Status] from ', QuoteName(table_name))
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'Status'

Related

Can I locate tables based on a column value? [duplicate]

This question already has answers here:
Find a value anywhere in a database
(18 answers)
Closed 5 years ago.
I am working without documentation we have a dental system that displays the status of an appointment.
I have to report on who scheduled the appt. and who confirmed. The systems displays this as 'FIRM' and 'FIXED. I have located how they store the person who scheduled the appt. but not who has confirmed it.
But since they use 'FIRM" is there a way I can locate which tables have this value? we are running sql server 2008.
you can find out the table name which have column name like FIRM by using below query -
select * from information_schema.columns where column_name like '%FIRM%'
if you want to know where this column is referred (like in SP , trigger or view) or a or hard coded value is used to display, you can use below query-
select distinct object_name(id) from syscomments where text like '%FIRM%'

Find out all useful columns in a table in sql server

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.

SQL Server - Constructing a Column Definition [duplicate]

This question already has answers here:
In SQL Server, how do I generate a CREATE TABLE statement for a given table?
(16 answers)
Closed 8 years ago.
I need to compare two databases and identify what columns have changed. Once I have identified a column that has changed in some way (size, type, etc) I need to capture (write to a table) the old column definition and the new column definition.
For instance, if using the INFORMATION_SCHEMA.COLUMNS table I discover that a column size has changed from 25 to 50 I will need to store the two column definition. In this case it may be 'char(25)' and 'char(50)'.
I have not problem using the INFORMATION_SCHEMA.COLUMNS table to identify when something has changed.
The issue I have is once I determine that the column has changed how do I build the column definition? In this case how to I build 'char(25)' and 'char(50)'?
Is there somewhere I can obtain this type of definition? If I have to build the definition piece by piece how do I determine all the components of the definition.
Any advice or suggestions are appreciated.
Thanks in advance.
select table_name, column_name,
column_definition = data_type + isnull('(' + convert(varchar, character_maximum_length) + ')', '')
from information_schema.columns

Row with unknown values to column SQL Server 2005

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

Possible to exclude or reorder a column from `*`? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
SQL exclude a column using SELECT * [except columnA] FROM tableA?
Is it possible to exclude a column from a select * from table statement with SQL Server?
I have a need for this and this is my only option other than parsing a raw SQL string to get out the required field names (I really don't want to do that).
Just to be bold. When the query is made I do not have access to the list of fields needed from the table but I do know which field I do not need. This is part of a complex multi-part query.
Surely there must be some way even if it's "hackish" such as using table variables or views
My other option is to reorder the columns. My problem is with ExecuteScalar SQL functions which get the first row and first column.
EDIT
I can't add an answer since this is now closed but the way I ended up doing it was like so:
;with results_cte as (
select (calculation) as calculated_column, * from table
)
select * into #temptable from results_cte
where calculated_column<10 /*or whatever*/
alter table #temptable
drop column calculated_column
select * from #temptable
drop table #temptable
Nope. You'll have to build your statement manually or just select *.
No.
Instead, you could check syscolumns to get all of the field names, or (perhaps) SELECT * and ignore that column.
If you use dynamic SQL, you can generate the query from metadata about the table or view (INFORMATION_SCHEMA.COLUMNS) and exclude columns that way. I do this a lot to generate triggers or views.
But there is nothing in the SQL language which supports this.
The best way to handle this would be to select * and then just not present the excluded column to your users in your frontend. As others have noted, SQL has no direct capability of doing an all-columns-except construct.