Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hii EveryOne,

Once more I need help from all of u..

I have Unknown numbers of Dynamic Tables in SQL DataBase

Just like Below,

=>User_Master
UserCode UserName
aaa	      AAA
bbb	      BBB

=>aaa_EmailInfo
UserCode	EmailId
aaa	      aaa@gmail.com
aaa	      aaa1@gmail.com

=>bbb_EmailInfo
UserCode	EmailId
bbb	      bbb@gmail.com
bbb	      bbb1@gmail.com
bbb	      bbb2@gmail.com


Declare @CheckEmail VARCHAR(500) = 'aaa@gmail.com'

SELECT
	
	User_Master.UserCode,
	
	(SELECT User_Master.UserCode+'_EmailInfo'.EmailId FROM User_Master.UserCode+'_EmailInfo') EmailId

FROM User_Master


Here,
User_Master is Static Table,
But aaa_EmailInfo and bbb_EmailInfo are Dynamically generated with Named on UsedCode..
Now i want to Check that @CheckEmail is Exist in Dynamic Tables on Not??

How is it Possible??

Can anyone Suggest me SELECT Query ???
Posted
Comments
Sinisa Hajnal 14-Nov-14 8:12am    
it may be objectname instead of name below, check the column names without the condition and adapt.

1 solution

Writing from memory, but it goes something like this:

SQL
SELECT *
FROM sys.tables WHERE name like '%_EmailInfo'
-- or any other search condition


SQL
DECLARE @email_tables table (id int IDENTITY (1,1), table_name varchar(255))

INSERT INTO @email_tables( table_name)
SELECT objectname FROM sys.tables WHERE name like '%_EmailInfo'

DECLARE @i int
SET @i = 0

DECLARE @sql_query nvarchar(MAX)
DECLARE @table_name varchar(255)
DECLARE @exists int
WHILE (SELECT TOP 1 FROM @email_tables WHERE id > @i ORDER BY id)
BEGIN
SELECT TOP 1 @i = id, @table_name = table_name FROM @email_tables ORDER BY id)

SET @sql_query = 'SELECT COUNT(*) FROM ' + @table_name + ' WHERE emailid = ''' + @CheckEmail + '''

EXEC @exists = sp_Execute @sql_query (you'll have to check exact syntax here )

END



If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
v2
Comments
Kiran Akabari 14-Nov-14 8:26am    
Ya friend,
It gives me Just all Table Names,
But how can i check datas of EmailId in all that tables??
Sinisa Hajnal 14-Nov-14 8:31am    
Create temporary table and fill it with the result of the above query. Then loop through the result and do IF EXISTS (your query to check for @Email)
Sinisa Hajnal 14-Nov-14 8:41am    
Updated solution.
Kiran Akabari 14-Nov-14 23:55pm    
I tried this but cant understand that while statement, and it also gives error in while statement..

Is their any change in it?? if yes then plz suggest me...
Sinisa Hajnal 17-Nov-14 2:00am    
While statement: it takes first lowest ID (which is ordered sequence number) greater then @i (which is initially zero) - it simply loops through all the records in @table_name. And "@table_name" should be "@email_tables". This variable holds the list of your EmailInfo tables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900