For example this is my ID: 07E485
I need to find this ID in all tables wherever it is found
All columns, which might carry this value, are sort of string-type...
Something like: select * from **alltables** where **anyColumn**='07E485'
The following query will return all tables in the database yourDBName whose name contains 07E485.
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND
TABLE_CATALOG = 'yourDBName' AND
TABLE_NAME LIKE '%07E485%'
If I misread your requirement, and you instead wanted to find all tables precisely named 07E485 in any database, then you can use the following query:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND
TABLE_NAME = '07E485'
In each database you have a view called INFORMATION_SCHEMA.COLUMNS, you can use this view to query through all of your tables.
This is the way I'ld do it, if anyone know a better way, feel free.. :)
SET NOCOUNT ON
DECLARE #Table varchar(255), #Schema varchar(255), #SQL varchar(MAX)
DECLARE table_cursor CURSOR FOR
SELECT TABLE_NAME, TABLE_SCHEMA
FROM INFORMATION_SCHEMA.COLUMNS -- This is a system view where you can see all columns of a database.
WHERE UPPER(COLUMN_NAME) = 'ID' -- This makes sure you don't loop through any tables that don't have a Column called 'ID'.
OPEN table_cursor
FETCH NEXT FROM table_cursor INTO #Table, #Schema
WHILE ##FETCH_STATUS = 0 BEGIN
-- This part creates your queries.
SET #SQL = 'SELECT * FROM '+#Schema+'.'+#Table+'
WHERE CAST(ID as varchar) = ''07E485''' -- Casting ID to varchar to avoid data type errors.
-- This executes the query.
EXEC(#SQL)
-- If a result is found, i.e. ID is equal to '07E485' somewhere in the table, Table name is printed on the "Messages" tab.
IF ##ROWCOUNT > 0 PRINT #Table
FETCH NEXT FROM table_cursor INTO #Table, #Schema
END
CLOSE table_cursor
DEALLOCATE table_cursor
To see which tables contain id = '07E485', go to "Messages" and you will have a list of them.
UPDATE My answer completely re-written
Try it like this: This dynamic SQL will check all string-type columns if they are equal to the given search string. You might want to add more data types to the output to get a better look onto the table's row. But one cannot simply put SELECT * as there are data types not allowed in XML without extra effort.
Secondly, by using QUOTENAME, I avoid syntax errors due to column or table names with blanks...
DECLARE #Search VARCHAR(10)='07E485';
DECLARE #cmd VARCHAR(MAX);
WITH TableNames AS
(
SELECT t.*
,t.TABLE_CATALOG + '.' + t.TABLE_SCHEMA + '.' + t.TABLE_NAME AS FullTblName
,QUOTENAME(t.TABLE_CATALOG)+ '.' + QUOTENAME(t.TABLE_SCHEMA) + '.' + QUOTENAME(t.TABLE_NAME) AS FullTblNameQuoted
,
STUFF(
(
SELECT 'OR ' + QUOTENAME(c.COLUMN_NAME) + '=''' + #Search + ''' '
FROM INFORMATION_SCHEMA.COLUMNS AS c
WHERE c.TABLE_CATALOG=t.TABLE_CATALOG AND c.TABLE_SCHEMA=t.TABLE_SCHEMA AND c.TABLE_NAME=t.TABLE_NAME
AND DATA_TYPE LIKE '%char%' --add more types if needed
FOR XML PATH('')
),1,3,'') AS WhereFilter
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE TABLE_TYPE='BASE TABLE'
)
SELECT #cmd = STUFF(
(
SELECT DISTINCT 'UNION ALL SELECT (SELECT ' + (SELECT STUFF((SELECT ',' + QUOTENAME(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS AS c
WHERE c.TABLE_CATALOG=TableNames.TABLE_CATALOG
AND c.TABLE_NAME =TableNames.TABLE_NAME
AND c.DATA_TYPE LIKE '%char%'
FOR XML PATH('')),1,1,'')) + ' FROM ' + FullTblNameQuoted
+ ' WHERE ' + WhereFilter
+ ' FOR XML PATH(''row''),ROOT(''' + REPLACE(REPLACE(FullTblName,'.','_'),' ','') + '''),TYPE) AS XmlData '
FROM TableNames
WHERE WhereFilter IS NOT NULL
FOR XML PATH('')
),1,10,'')
SET #cmd='SELECT XmlData FROM(' + #cmd + ') AS tbl WHERE XmlData IS NOT NULL;'
PRINT LEN(#cmd)
EXEC(#cmd)
Related
I have 1000s of tables like ABC_0001, ABC_0002, ABC_0003 and so on.. and I would like to do 2 things:
From each table, I am looking to copy columns x, y and z in new table.
However not all tables have all 3 columns e.g. ABC_0001 is missing column x, ABC_0002 is missing y and z or any other combinations.
How can I check if the columns is present in selected table and if not assign default value say "0".
There are actually many variables instead of x, y, z which needs to be copied from all the tables into new table. I have this list in a separate table (say TABLE_COL_NAME with variable name, var type e.g. a int, b varchar(200) and so on) and this will change in future, hence I would like to create NEW_TABLE and assign variables in #VarNames dynamically using TABLE_COL_NAME.
Thanks Annamalai for helping with below base code. cheers.
Edit: This is SSMS v18.7
DECLARE
#TableName VARCHAR(500)
,#Sql NVARCHAR(MAX) = ''
,#Id INT
,#VarNames VARCHAR(8000)
DROP TABLE IF EXISTS TABLE_NEW
CREATE TABLE TABLE_NEW (x int, y varchar(200), z varchar(500))
SET #VarNames = 'x, y, z'
DECLARE Table_Cursor CURSOR
FOR
SELECT
ROW_NUMBER() OVER (ORDER BY TABLE_NAME ASC) Id
,TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME LIKE 'ABC%'
OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO #Id,#TableName
WHILE ##FETCH_STATUS = 0
BEGIN
IF(#Id = 1)
BEGIN
SET #Sql = #Sql + 'SELECT ' + #VarNames + 'FROM '+#TableName ----Modify the columns based on your column names
SELECT #SQL
END
ELSE
BEGIN
SET #Sql = #Sql + ' UNION ALL SELECT '+ #VarNames + 'FROM '+#TableName --Modify the columns based on your column names
END
FETCH NEXT FROM Table_Cursor INTO #Id,#TableName
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor
INSERT INTO TABLE_NEW
EXEC (#Sql)
You have a lousy data model. It looks like you need some code to help fix it.
You can construct a union all statement as follows. First, construct the appropriate logic for the column names for each table:
select 'select ' +
coalesce(max(case when c.column_name = 'x' then c.column_name end), 'null as x') + ', ' +
coalesce(max(case when c.column_name = 'y' then c.column_name end), 'null as y') + ', ' +
coalesce(max(case when c.column_name = 'z' then c.column_name end), 'null as z') + ' ' +
'from ' + c.table_name
from information_schema.columns c
where table_name like 'abc_%';
group by c.table_name;
Then construct the SQL statement:
select string_agg(sql,
'
union all
'
)
from (select 'select ' +
coalesce(max(case when c.column_name = 'x' then c.column_name end), 'null as x') + ', ' +
coalesce(max(case when c.column_name = 'y' then c.column_name end), 'null as y') + ', ' +
coalesce(max(case when c.column_name = 'z' then c.column_name end), 'null as z') + ' ' +
'from ' + c.table_name as sql
from information_schema.columns c
where table_name like 'abc_%'
group by c.table_name
) t;
You can then assign this to a variable and run it. A SQL Fiddle illustrates this.
Note that the above is intentionally simplified to focus on the steps, not taking into account:
The _ in the table name is a wildcard for like.
The schema name as well as the table name.
Column names that need to be escaped.
You can also use the constructed SQL to create a view rather than execute it directly.
I am trying to set a default value to a column(Inserted_time), but first i need to check if the column exists in the tables. If the column doesn't exist, I need to add that column and give it a default value.
I am working with Sql Server Management Studio.
So far I have written this code:
IF EXISTS ( select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG = 'DB_COPY' and COLUMN_NAME = 'Inserted_Time')
begin
ALTER TABLE table_name ADD CONSTRAINT [Inserted_Time_Def] SET DEFAULT (sysdatetimeoffset()) FOR [Inserted_Time]
end
else
ALTER TABLE table_name ADD COLUMN [Inserted_Time] CONSTRAINT [Inserted_Time_Def] DEFAULT (sysdatetimeoffset()) WITH VALUES
Once I retrieve the tables that has the column, I need to add that table_name to the Alter command. But I am not able to do that. Can someone please tell me how to use the table_names retrieved from select statement in the alter statement?
First, you want to put all the table names in a temporary table so you can loop through it.
After, you can use a cursor to execute a command for each table name.
In my example, I only printed the command I wanted to execute. That way you can be sure the code will do what you want first.
Example :
select TABLE_NAME As TableName INTO #TablesList from INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG = 'DB_COPY' and COLUMN_NAME = 'Inserted_Time'
DECLARE #TablesCursor as CURSOR;
DECLARE #TableName as NVARCHAR(max);
DECLARE #CommandToExecute as NVARCHAR(max);
SET #TablesCursor = CURSOR FOR SELECT TableName FROM #TablesList;
OPEN #TablesCursor;
FETCH NEXT FROM #TablesCursor INTO #TableName;
WHILE ##FETCH_STATUS = 0
BEGIN
SET #CommandToExecute = 'ALTER TABLE ' + #TableName + ' WHAT YOU WANNA DO '
PRINT #CommandToExecute
--EXEC(#CommandToExecute)
FETCH NEXT FROM #TablesCursor INTO #TableName;
END
CLOSE #TablesCursor;
DEALLOCATE #TablesCursor;
Assuming that every table is in a different schema, then you could do something like this:
DECLARE #SQL nvarchar(MAX);
SET #SQL = STUFF((SELECT NCHAR(13) + NCHAR(10) +
CASE WHEN EXISTS (SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE T.TABLE_SCHEMA = C.TABLE_SCHEMA
AND T.TABLE_NAME = C.TABLE_SCHEMA
AND C.COLUMN_NAME = N'Inserted_Time') THEN N'ALTER TABLE ' + QUOTENAME(T.TABLE_SCHEMA) + N'.' + QUOTENAME(T.TABLE_NAME) + N' ADD CONSTRAINT [Inserted_Time_Def] DEFAULT (sysdatetimeoffset()) FOR [Inserted_Time];'
ELSE N'ALTER TABLE ' + QUOTENAME(T.TABLE_SCHEMA) + N'.' + QUOTENAME(T.TABLE_NAME) + N' ADD COLUMN [Inserted_Time] CONSTRAINT [Inserted_Time_Def] DEFAULT (sysdatetimeoffset());'
END
FROM INFORMATION_SCHEMA.TABLES T
WHERE T.TABLE_CATALOG = N'DB_COPY'
FOR XML PATH(N''),TYPE).value('.','nvarchar(MAX)'),1,2,N'');
PRINT #SQL; --Your best friend. If more than 4,000 characters, use SELECT
EXECUTE sp_executesql #SQL;
This will very likely hugely out perform a CURSOR solution if you have a large number of schemas.
Having a simple query such as:
select * from foo
Where the output has the columns
bar | abc | def
---------------
...............
It is possible to encapsulate this query into another one and give sequential names instead to the columns such as 1|2|3... (mantaining all the rows intact)?
I know this sounds weird and probably it isn't even possible.
Thanks for the help!
Try This
DECLARE #Sql NVARCHAR(max),
#TableName VARCHAR(100) = '<YourTableName>'
;WITH CTE
AS
(
SELECT DENSE_RANK()OVER (ORDER BY COLUMN_NAME) Rno,
TABLE_NAME,
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG='Database'
AND TABLE_NAME = #TableName
)
SELECT 'SELECT '+ STUFF((SELECT ', '+ COLUMN_NAME +' AS '+ QUOTENAME(CAST(Rno AS VARCHAR))
FROM CTE
FOR XML PATH ('')),1,1,'') + ' FROM '+#TableName
PRINT #Sql
EXEC (#Sql)
Try this:
DECLARE #TableName NVARCHAR(100) = 'YOUR TABLENAME HERE',
#Query NVARCHAR(MAX) = 'SELECT '
SELECT #Query = CONCAT(#Query, ' ', COLUMN_NAME, ' AS ', QUOTENAME(ROW_NUMBER() OVER(ORDER BY ORDINAL_POSITION)), ',
')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = #TableName
SET #Query = SUBSTRING(#Query, 0, LEN(#Query) - 3) + '
FROM ' + QUOTENAME(#TableName)
You use INFORMATION_SCHEMA.COLUMNS to get all our table's columns,
ROW_NUMBER() window function to generate sequential number for column aliases and sp_ExecuteSQL to execute dynamic generated queries.
My requirement is to compare data between two environments and i there is diff in both tables of both environments, insert that data to a temp table and display it.The above solution is not suiting for my scenario. I will explain my scenario in a better way.
In a Curor Cur1, I have all data of DEV from a Table(Report) where Rep_ID=1, Getting corresponding data from the TEST of REPORT Table where Rep_ID=1 In a while loop I am comparing the data of DEV and TEST
if (#DevData1 <> #TestData1)
BEGIN Get ColumnName from Report table where #DevData1 =1 Insert Into #TempTable (ColumnName, DevData1, TestData1)
ENDS Cur1 Ends
When I try to get the column name for a varchar column, I am getting the column name properly with the below query
Declare #ColStrRep nvarchar(1000)= 'select #retValOut= Col.value(''local-name(.)'', ''varchar(max)'') from (select * from Rep_attr where Rep_Name = '''+#reptName +''' for xml path(''''), type) as T(XMLCol) cross apply T.XMLCol.nodes(''*'') as n(Col) where Col.value(''.'', ''varchar(100)'') = '+#reptName +''
print #ColStrRep
EXEC Sp_executesql #ColStrRep,N'#retValOut nvarchar(100) out',#Column_Name OUT
But when I try to get the columnName for an integer column, and that too when we have the same value as 1 in the table( like RepID=1, Flag=1 , IsEmpty=1 etc), the query is getting confused and instead of Rep_ID, it retrieves the column IsEmpty. SO I need another query which just give me the columnname for a columnValue.
Thanks and Regards,
Sajitha
This solution will search using LIKE operator for varchar columns (i.e. column like '%5%') and a strict value for int columns (i.e. column=5)
DECLARE #table_name SYSNAME = 'your_table',
#search_string VARCHAR(100) = '5', --what to search
#column_name SYSNAME,
#type_name SYSNAME,
#sql_string VARCHAR(2000)
BEGIN TRY
DECLARE columns_cur CURSOR
FOR
SELECT columns.name, types.name type_name FROM sys.columns
JOIN sys.types ON columns.system_type_id = types.system_type_id
JOIN sys.objects ON columns.object_id=objects.object_id
WHERE objects.type = 'U' AND objects.name=#table_name
AND types.name IN ('varchar', 'nvarchar', 'int', 'bigint', 'smallint') --types of columns which you want to use for search
OPEN columns_cur
FETCH NEXT FROM columns_cur INTO #column_name, #type_name
WHILE (##FETCH_STATUS = 0)
BEGIN
IF #type_name IN ( 'varchar', 'nvarchar')
SET #sql_string = 'IF EXISTS (SELECT * FROM ' + #table_name + ' WHERE [' + #column_name + '] LIKE ''%' + #search_string + '%'') RAISERROR(''' + #table_name + ', ' + #column_name + ''',0,1) WITH NOWAIT'
ELSE
SET #sql_string = 'IF EXISTS (SELECT * FROM ' + #table_name + ' WHERE [' + #column_name + '] = TRY_CAST(''' + #search_string + ''' AS '+ #type_name +')) RAISERROR(''' + #table_name + ', ' + #column_name + ''',0,1) WITH NOWAIT'
EXECUTE(#sql_string)
FETCH NEXT FROM columns_cur INTO #column_name, #type_name
END
CLOSE columns_cur
DEALLOCATE columns_cur
END TRY
BEGIN CATCH
CLOSE columns_cur
DEALLOCATE columns_cur
RAISERROR(' - No access to table %s',0,1,#table_name) WITH NOWAIT
END CATCH
Thanks for the suggestion.
But I could manage the situation with the below query.
In case, the column value is a varchar, then the below query gives me the column Name.
Declare #ColStrDesc nvarchar(1000)= 'select
#retValOut= Col.value(''local-name(.)'', ''varchar(max)'')
from (select *
from Rep_attr
where Rep_Name = '''+#reptName +'''
for xml path(''''), type) as T(XMLCol)
cross apply
T.XMLCol.nodes(''*'') as n(Col)
where Col.value(''.'', ''varchar(100)'') = '''+#rep_Desc +''''
print #ColStrDesc
EXEC Sp_executesql #ColStrDesc,N'#retValOut nvarchar(100) out',#Column_Name OUT
In case, the column Value is an integer , thenn below query gives me column name.
Declare #ColErr nvarchar(1000)= 'SELECT #retValOut= STUFF(''''
+ CASE WHEN EXISTS (SELECT 1 FROM [dbo].[Rep_attr] WHERE Cast([Rep_Errs] as VARCHAR(64)) = '+#rep_Errs+') THEN '' Rep_Errs'' ELSE '''' END , 1, 1, '''')'
EXEC Sp_executesql #ColErr,N'#retValOut nvarchar(100) out',#Column_Name OUT
I have a procedure that does a "select * from view" and this output contains an inverted comma in one of the columns. I want to do a replace() to remove the extra inverted comma but I cannot change the "select *" as this is a dynamic query that is used in different scenarios.
As evident, the following query does not work
select replace(*, '"', '') from ReportRegistry
Some help appreciated.
I wanted to know if there is any way I can remove the quotes without knowing the column names. I want them to be removed from all columns. The columns names are different in different cases.
The view is defined within the procedure differently in different cases. The dynamic colum names are added as text and them executed using sp_executesql.
DECLARE #ColNames NVARCHAR(MAX) = ''
--Dynamically build a list of column names for the view, separated by commas.
SELECT #ColNames = #ColNames +
CASE
--Use the REPLACE function for "String" type data types. Did I leave any data types out?
--Change "x" to the character(s) you want to replace.
WHEN c.DATA_TYPE IN ('VARCHAR', 'CHAR', 'NVARCHAR', 'NCHAR') THEN 'REPLACE([' + c.COLUMN_NAME + '], ''x'', '''') AS [' + c.COLUMN_NAME + '],'
--All other data types.
ELSE '[' + c.COLUMN_NAME + '],'
END
--In addition to user tables, views are included in INFORMATION_SCHEMA.COLUMNS (and in INFORMATION_SCHEMA.TABLES).
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_NAME = 'ReportRegistry'
--Retain original order of the view's columns.
ORDER BY c.ORDINAL_POSITION
--Remove last comma
SET #ColNames = LEFT(#ColNames, LEN(#ColNames) - 1)
EXEC ('SELECT ' + #ColNames + ' FROM ReportRegistry ')
select replace(TargetColumnName, '"', '')
,Column2
,Column3
,..... and so on.....
from ReportRegistry
declare #tbl varchar(max) = 'table_or_view_name'
declare #cols varchar(max) =
(select case when data_type='varchar' then 'REPLACE('+column_name+',''"'','''')' else column_name end+char(10)+','
from information_schema.columns
where table_name = #tbl for xml path(''))
declare #sql varchar(max)='select '+left(#cols,len(#cols)-1)+' from '+#tbl
print #sql
exec(#sql)