Select all values from all tables with specific table name - sql

EDIT original question:
Our UDW is broken out into attribute and attribute list tables.
I would like to write a data dictionary query that dynamically pulls in all column values from all tables that are like %attr_list% without having to write a series of unions and update or add every time a new attribute list is created in our UDW.
All of our existing attribute list tables follow the same format (number of columns, most column names, etc). Below is the first two unions in our existing view which I want to avoid updating each time a new attribute list table is added to our UDW.
CREATE VIEW [dbo].[V_BI_DATA_DICTIONARY]
( ATTR_TABLE
,ATTR_LIST_ID
,ATTR_NAME
,ATTR_FORMAT
,SHORT_DESCR
,LONG_DESCR
,SOURCE_DATABASE
,SOURCE_TABLE
,SOURCE_COLUMN
,INSERT_DATETIME
,INSERT_OPRID
)
AS
SELECT 'PREAUTH_ATTR_LIST' ATTR_TABLE
,[PREAUTH_ATTR_LIST_ID] ATTR_LIST_ID
,[ATTR_NAME] ATTR_NAME
,[ATTR_FORMAT] ATTR_FORMAT
,[SHORT_DESCR] SHORT_DESCR
,[LONG_DESCR] LONG_DESCR
,[SOURCE_DATABASE] SOURCE_DATABASE
,[SOURCE_TABLE] SOURCE_TABLE
,[SOURCE_COLUMN] SOURCE_COLUMN
,[INSERT_DATETIME] INSERT_DATETIME
,[INSERT_OPRID] INSERT_OPRID
FROM [My_Server].[MY_DB].[dbo].[PREAUTH_ATTR_LIST]
UNION
SELECT 'SAVINGS_ACCOUNT_ATTR_LIST'
,[SAVINGS_ACCOUNT_ATTR_LIST_ID]
,[ATTR_NAME]
,[ATTR_FORMAT]
,[SHORT_DESCR]
,[LONG_DESCR]
,[SOURCE_DATABASE]
,[SOURCE_TABLE]
,[SOURCE_COLUMN]
,[INSERT_DATETIME]
,[INSERT_OPRID]
FROM [My_Server].[MY_DB].[dbo].[SAVINGS_ACCOUNT_ATTR_LIST]'

Something like this might work for you if all tables contain the same columns.
Just change the temp table and the selected columns to match your own columns.
CREATE TABLE #results (
ATTR_TABLE SYSNAME,
ATTR_LIST_ID INT,
ATTR_NAME NVARCHAR(50),
ATTR_FORMAT NVARCHAR(50),
SHORT_DESCR NVARCHAR(50),
LONG_DESCR NVARCHAR(255),
SOURCE_DATABASE NVARCHAR(50),
SOURCE_TABLE NVARCHAR(50),
SOURCE_COLUMN NVARCHAR(50),
INSERT_DATETIME DATETIME,
INSERT_OPRID INT
);
INSERT INTO #results
EXEC sp_MSforeachtable #command1 =
'
SELECT ''?''
, *
FROM ?
WHERE ''?'' LIKE ''%ATTR_LIST%''
'
SELECT *
FROM #results
DROP TABLE #results
EDIT: Updated my example with your columns. Because you use different column name for ATTR_LIST_ID in each table I changed the select to SELECT *. Obviously I don't know the data types of your columns so you have to change them.
This won't work in a view but you could create a stored procedure.

For SQL Server you should be able to use something like this:
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE t.name LIKE '%attr_list%'
And this will include views as well as tables
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%attr_list%'

If using MS SQL Server check out the sys catalog views. You can use sys.tables and join to sys.columns to get your tables and columns. sys.extended_properties can get you description information, if entered.

Related

View's column contains data which looks like UUID but the column type is int. Why?

I've access to a view on a SQL Server 2016 database.
The column named 'id_key' contains such data:
id_key
D93F37FC-3C2A-EB11-B813-00505690E502
B03D37FC-3C2A-EB11-B813-00505690E502
AC644CFC-3C2A-EB11-B813-00505690E502
I've checked the type of the column: it's int
Truly, the result of:
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'yourTableName' AND
COLUMN_NAME = 'yourColumnName'
returns just int.
I've not found any explanation for that in SQL Server 2016 docs.
Have I missed something?
How int type store data which looks like strings/uuids?
If the view was not created using the WITH SCHEMABINDING option then the underlying tables that it references are freely able to change.
It is possible that the problematic column was originally using an int data type when the view was created but has subsequently changed to uniqueidentifier, e.g.:
drop view if exists dbo.yourViewName;
drop table if exists dbo.yourTableName;
go
create table dbo.yourTableName (
ignore int,
yourColumnName int
);
go
create view dbo.yourViewName --with schemabinding
as
select yourColumnName as id_key
from dbo.yourTableName
go
alter table dbo.yourTableName
drop column yourColumnName
go
alter table dbo.yourTableName
add yourColumnName uniqueidentifier
go
insert dbo.yourTableName (yourColumnName) values
('D93F37FC-3C2A-EB11-B813-00505690E502'),
('B03D37FC-3C2A-EB11-B813-00505690E502'),
('AC644CFC-3C2A-EB11-B813-00505690E502')
go
select * from dbo.yourViewName
go
select data_type
from information_schema.columns
where table_name = 'yourViewName'
and column_name = 'id_key'
Which yields:
id_key
------------------------------------
D93F37FC-3C2A-EB11-B813-00505690E502
B03D37FC-3C2A-EB11-B813-00505690E502
AC644CFC-3C2A-EB11-B813-00505690E502
data_type
----------
int
See the CREATE VIEW (Transact-SQL) documentation for more information.

TSQL - Identify the table name as a column of the single or multiple tables in a select statement WITHOUT HARDCODING it as part of the query

I need to display the table name in my select statement.
select col1, table_name_1 as table_name from table_name_1
Which I insert into another table.
My problem is I have to do this dynamically not as explained by this question.
Display the table name in the select statement
Not sure if this available with a single key word like for an eg. the currentdate we could use something like GETDATE().
Thank you for your answer.
As you were told already, there is no special keyword, which you could use for an easy-cheesy solution. If you really need this, you would add the table's name as a parameter and use a dynamically created statement.
Another approach was a VIEW for each table you are looking for, something along this
CREATE VIEW Table1_withName AS
SELECT 'Table1' AS TableName, * FROM Table1 --use columns instead of "*"
You can use this VIEW instead of the table in all your selects. This is not without hardcoding, but you could even generate the code to create your views out of INFORMATION_SCHEMA.TABLES. And when you call the views, there is no special action needed.
You might use FOR XML AUTO...
There is a fully generic / ad-hoc approach too, but I doubt that this will help you:
Hint: I use - as example only - a TOP 3 * FROM sys.objects. This would work with your tables too.
--FOR XML AUTO will add the table's name as the element's name
SELECT TOP 3 * FROM sys.objects FOR XML AUTO
--result
<sys.objects name="sysrscols" object_id="3" ...
--In case you use an alias, this alias is used
SELECT TOP 3 * FROM sys.objects AnyAlias FOR XML AUTO
--You would have to change this a little, if you want to enforce to include NULL values.
--But this is element centric XML now, the one before was attribute centric.
SELECT TOP 3 * FROM sys.objects FOR XML AUTO,ELEMENTS XSINIL
--This is the way you could use this, to simulate the generic table's name
SELECT EachRow.value('local-name(.)','nvarchar(max)') AS TableName
,EachRow.value('./#name,','nvarchar(max)') AS name
,EachRow.value('./#object_id','int') AS object_id
,EachRow.value('./#schema_id','int') AS schema_id
--add all columns here
FROM
(
--Your original Statement goes here
SELECT TOP 3 * FROM sys.objects
FOR XML AUTO, TYPE
) A(TheSelectAsXml)
CROSS APPLY A.TheSelectAsXml.nodes('*') B(EachRow);
The result
TableName name object_id schema_id
sys.objects sysrscols 3 4
This would even work with joined sources. Try this one out:
SELECT TOP 3 * FROM sys.objects
INNER JOIN sys.all_columns ON sys.objects.object_id=sys.all_columns.object_id
FOR XML AUTO,TYPE
I don't think there is inbuilt function available in T-SQL other than OBJECT_NAME (object_id('TableName')) which required to hard-code Table name.
However, as workaround, following query could fulfill your requirements:
Declare #tbname varchar(50);
Declare #SelectQuery varchar(2000);
set #tbname = 'YourTable';
set #SelectQuery = 'select *, OBJECT_NAME (object_id(' + '''' + #tbname + '''' +')) from ' + #tbname;
--print #SelectQuery;
exec (#SelectQuery);

Get column from a table dynamically on basis of input parameter

I want to create a stored procedure in which I will pass an id and I want to select a column on basis of it.
Suppose my table has columns namely - id, theme1, theme2, theme3, theme4, theme5 etc.
I need a query which will select the column on basis of my input. Like if I pass id = 3 then it should give theme3 column as result.
Is there any way possible for this type of dynamic query?
Suppose my table has columns namely - id, theme1, theme2, theme3, theme4, theme5 etc.
If you can change this you really should...
Whenever you feel the need to set a number to a column's name (like Tel1, Tel2 or Theme1, Theme2) this is a very strong hint for a bad design...
If possible try something like
CREATE TABLE Theme(ThemeID INT IDENTITY, ForeignID INT, ThemeRank INT, ThemeName VARCHAR(100));
SELECT *
FROM YourTable AS yt
INNER JOIN Theme AS th ON yt.ForeignID=th.ForeignID AND ThemeRank=#passedInValue;
To answer your question: The only way to do this is dynamically created SQL and I would really not recommend it! This is a very bad approach...
UPDATE Just to be complete:
DECLARE #passedInId INT=3;
DECLARE #cmd VARCHAR(MAX)='SELECT Theme' + CAST(#passedInId AS VARCHAR(MAX)) + ' FROM YourTable WHERE ...';
EXEC(#cmd);
This will create a SQL command dynamically and execute it. The problems are
no ad-hoc / inline approach
The usage of the retrieved result is - uhm - cumbersome...
I think this will help you,
Ordinal_Position will help you to find the column details
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
AND ORDINAL_POSITION = 2
https://www.mssqltips.com/sqlservertutorial/183/informationschemacolumns/

How to select all the columns of a table except one column?

How to select all the columns of a table except one column?
I have nearly 259 columns I cant mention 258 columns in SELECT statement.
Is there any other way to do it?
You can use this approach to get the data from all the columns except one:-
Insert all the data into a temporary table
Then drop the column which you dont want from the temporary table
Fetch the data from the temporary table(This will not contain the data of the removed column)
Drop the temporary table
Something like this:
SELECT * INTO #TemporaryTable FROM YourTableName
ALTER TABLE #TemporaryTable DROP COLUMN Columnwhichyouwanttoremove
SELECT * FROM #TemporaryTable
DROP TABLE #TemporaryTable
Create a view. Yes, in the view creation statement, you will have to list each...and...every...field...by...name.
Once.
Then just select * from viewname after that.
This is not a generic solution, but some databases allow you to use regular expressions to specify the columns.
For instance, in the case of Hive, the following query selects all columns except ds and hr:
SELECT `(ds|hr)?+.+` FROM sales
You can get the column name details from sys.columns table
Try the following query:
SELECT * FROM SYS.COLUMNS
WHERE object_id = OBJECT_ID('dbo.TableName')
AND [Name] <> 'ColumnName'
DECLARE #sql as VARCHAR(8000)
SET #sql = 'SELECT '
SELECT #sql += [Name] + ', ' FROM SYS.COLUMNS
WHERE object_id = OBJECT_ID('dbo.TableName')
AND [Name] <> 'ColumnName'
SELECT #sql += ' FROM Dbo.TableName'
EXEC(#sql)
I just wanted to echo #Luann's comment as I use this approach always.
Just right click on the table > Script table as > Select to > New Query window.
You will see the select query. Just take out the column you want to exclude and you have your preferred select query.
There are lot of options available , one of them is :
CREATE TEMPORARY TABLE temp_tb SELECT * FROM orig_tb;
ALTER TABLE temp_tb DROP col_x;
SELECT * FROM temp_tb;
Here the col_x is the column which u dont want to include in select statement.
Take a look at this question : Select all columns except one in MySQL?
You can retrieve the list of column name by simple query and then remove those column by apply where query like this.
SELECT * FROM (
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'TableName'
) AS allColumns
WHERE allColumns.COLUMN_NAME NOT IN ('unwantedCol1', 'unwantedCol2')
If you are using DataGrip you can do the following:
Enter your SELECT statement SELECT * FROM <your_table>;
Put your cursor over * and press Alt+Enter
You will get pop up menu with Expand column list option
Click on it and it will convert * with full list of columns
Now you can remove columns that you don't need
Here is a link for an example on how to do it.
Without creating new table you can do simply (e.g with mysqli):
get all columns
loop through all columns and remove wich you want
make your query
$r = mysqli_query('SELECT column_name FROM information_schema.columns WHERE table_name = table_to_query');
$c = count($r); while($c--) if($r[$c]['column_name'] != 'column_to_remove_from_query') $a[] = $r[$c]['column_name']; else unset($r[$c]);
$r = mysqli_query('SELECT ' . implode(',', $a) . ' FROM table_to_query');
Try the following query:
DECLARE #Temp NVARCHAR(MAX);
DECLARE #SQL NVARCHAR(MAX);
SET #Temp = '';
SELECT #Temp = #Temp + COLUMN_NAME + ', ' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='Person' AND COLUMN_NAME NOT IN ('Id')
SET #SQL = 'SELECT ' + SUBSTRING(#Temp, 0, LEN(#Temp)) +' FROM [Person]';
EXECUTE SP_EXECUTESQL #SQL;
In your case, expand columns of that database in the object explorer. Drag the columns in to the query area.
And then just delete one or two columns which you don't want and then run it. I'm open to any suggestions easier than this.
Only one way to achieve this giving column name. There is no other method found. You must have to list all column name

MERGE Command in SQL Server

I have been using the statement
insert into target
select * from source
where [set of conditions] for a while.
Recently found this MERGE command that will be more effective to use for my purpose so that I can change the above statement to
MERGE target
USING source ON [my condtion]
WHEN NOT MATCHED BY TARGET
THEN INSERT VALUES (source.col1, source.col2, source.col3)
But the problem for me is lets say if I have 20+ columns in my source table I have to list all of them, I need a way to specify it to insert source.* . Is there a way ? I'm new to SQL. Appreciate your help.
Thanks in advance :)
Me too; I hate typing column names.
I normally build the Merge statement in dynamic SQL.
I have a function that takes a table name as a parameter, and returns a string containing all column names formatted properly with Table Name prefix, [] brackets and comma, as in S.Col1, S.Col2, S.Col3
I could also tell you that I build a temp table with the required columns, and pass the temp table to my function, because some times you don't want a list of all columns. But that would probably be a confusing wooble, obscuring the important bits;
Use dynamic sql
Use a function to create csv list of columns.
Everything that I have read regarding the MERGE statement says that you need to specify the columns for your INSERT statement. If you are looking for a quick way to get the INSERT statment, you can right mouse click the table in SSMS and select Script Table As->INSERT To->Clipboard. You can then paste this into your query and alter just the VALUES part.
Merge statement
There's simply no advantage of using MERGE in this situation. Why overcomplicate? Stick to the KISS principle, for chrissake.
Anyways, here's the script:
declare
#targetTableName varchar(100) = 'target'
,#targetSchemaName varchar(20) = 'dbo'
,#sourceTableName varchar(100) = 'source'
,#sourceSchemaName varchar(20) = 'dbo2'
,#matchCondition varchar(50) = 't.id = s.id'
,#columns varchar(max)
set #columns = (select ','+quotename(c.name)
from sys.tables t
join sys.columns as c on t.object_id = c.object_id
join sys.schemas s on s.schema_id = t.schema_id
where t.name = #targetTableName and s.name = isnull(#targetSchemaName, s.name)
for xml path(''))
--a column name starts with a comma
declare #sql varchar(max) = '
merge #target t
using #source s on #matchCondition
when not matched then
insert (#columns)
values #sourceColumns'
set #sql =
replace(replace(replace(replace(replace(#sql
, '#matchCondition', #matchCondition)
--replace #columns with column list with the first comma removed
, '#columns', stuff(#columns, 1, 1, ''))
--replace #sourceColumns with column list with the 's.' prefix and comma removed
, '#sourceColumns', stuff(replace(#columns, ',', ',s.'),1,1,''))
, '#target', quotename(#targetSchemaName)+'.'+quotename(#targetTableName))
, '#source', quotename(#sourceSchemaName)+'.'+quotename(#sourceTableName))
print #sql
--exec(#sql)
And we'll get something like this:
merge [dbo].[target] t
using [dbo2].[source] s on t.id = s.id
when not matched then
insert ([column1], [column2], [column3], [column4])
values s.[column1], s.[column2], s.[column3], s.[column4]