SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


tsql to get table name from a regular dml query


tsql to get table name from a regular dml query

Author
Message
Siten0308
Siten0308
SSC-Addicted
SSC-Addicted (496 reputation)SSC-Addicted (496 reputation)SSC-Addicted (496 reputation)SSC-Addicted (496 reputation)SSC-Addicted (496 reputation)SSC-Addicted (496 reputation)SSC-Addicted (496 reputation)SSC-Addicted (496 reputation)

Group: General Forum Members
Points: 496 Visits: 347
Hey guys,

need your briliant minds, sadly i dont have one Sad and sorry for the confused title.

ok i have a select statement like:

select OrderID, customer name, blah blah
from dbo.orders

however i am using this select statement in BIML, but dont let that throw anyone off, i am trying to say:

select *
from dbo.orders
where orders in
(
select name
from sys.tables
where name like 'orders'
)

therefore it will complete the select statement, however if it wasnt an order table, then skip the select statement, i am really sorry if i am not explaining it right, but that is what i can think on top of my head how i can accomplish my task that BIML requires.

what the main purpose is to dynamically do select statements for only certain tables in a database, if it meets my criteria, then do the select statement, if not, skip it.... is this possible?

thanks in advance
Phil Parkin
Phil Parkin
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34067 Visits: 20854
Siten0308 - Monday, March 20, 2017 4:21 PM
Hey guys,

need your briliant minds, sadly i dont have one Sad and sorry for the confused title.

ok i have a select statement like:

select OrderID, customer name, blah blah
from dbo.orders

however i am using this select statement in BIML, but dont let that throw anyone off, i am trying to say:

select *
from dbo.orders
where orders in
(
select name
from sys.tables
where name like 'orders'
)

therefore it will complete the select statement, however if it wasnt an order table, then skip the select statement, i am really sorry if i am not explaining it right, but that is what i can think on top of my head how i can accomplish my task that BIML requires.

what the main purpose is to dynamically do select statements for only certain tables in a database, if it meets my criteria, then do the select statement, if not, skip it.... is this possible?

thanks in advance

Dynamically, based on what?

Do you want a loop which iterates round all tables containing the word 'Orders' and selects all rows from them?

Like 'Orders' is the same as = 'Orders', because you have not included any wildcards.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Joe Torre
Joe Torre
Right there with Babe
Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)

Group: General Forum Members
Points: 795 Visits: 356
I think what you want is something like:

IF EXISTS(SELECT * FROM sys.tables WHERE name = 'Orders')
SELECT *
FROM Orders;

Ed Wagner
Ed Wagner
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28141 Visits: 10593
Siten0308 - Monday, March 20, 2017 4:21 PM
Hey guys,

need your briliant minds, sadly i dont have one Sad and sorry for the confused title.

ok i have a select statement like:

select OrderID, customer name, blah blah
from dbo.orders

however i am using this select statement in BIML, but dont let that throw anyone off, i am trying to say:

select *
from dbo.orders
where orders in
(
select name
from sys.tables
where name like 'orders'
)

therefore it will complete the select statement, however if it wasnt an order table, then skip the select statement, i am really sorry if i am not explaining it right, but that is what i can think on top of my head how i can accomplish my task that BIML requires.

what the main purpose is to dynamically do select statements for only certain tables in a database, if it meets my criteria, then do the select statement, if not, skip it.... is this possible?

thanks in advance

First let me tell you that I don't know BIML.

That said, why would you want to do this? If you already know the names of the tables, you can query them. If you don't know the names of the tables, why would you want to query them?



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
matthew.peters 38648
matthew.peters 38648
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 19
If you want to select records from all tables that exist where the table name is like ‘orders’ you would need to iterate through a list of table names that meet the criteria, pass in the table name as a variable, then run a select statement using the variable as a table name. To do this you would need to use a loop.

declare @name as varchar(128)
DECLARE cur CURSOR
FOR
select name from sys.tables
where name like '%order%'

OPEN cur FETCH NEXT FROM cur INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN

--I have used a top 5 condition to limit results
exec('select top 5 * from ' + @name)

FETCH NEXT FROM cur INTO @name
END
CLOSE cur
DEALLOCATE cur

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search