Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have 4 tables with the same attribute/column name.

T1
Name	Age	Town
A	22	Berlin
B	18	Hamburg
C	30	New York

T2
Name	Age	Town
A	22	Berlin
D	25	Atlanta
F	21	Accra

T3
Name	Age	Town
E	45	Madrid
B	18	Hamburg
C	30	New York

T4
Name	Age	Town
Z	17	Zurich
E	45	Madrid
S	28	Potsdam

I want to create a sp PROC to display the difference (*new entry ) two columns whenever a user chooses a table dynamically.
(*new entry ) = T2 :T1 what’s new in T2 compared to T1
T4:T2, T3:T3 (dynamically)

Here my sql statement but it only chooses the predefined tables.
SQL
CREATE PROC spNewEntrance
	
	(
	@ NewEntrance1 AS NVARCHAR (255),
	@ NewEntrance2 AS NVARCHAR (255)

	)
AS
BEGIN
	
	SET NOCOUNT OFF;
	SELECT  [Age] AS Age, [Town] AS City
	FROM [dbo].[T2]
EXCEPT
	SELECT  [Age] AS Age, [Town] AS City	
FROM [dbo].[T1]


END
GO
EXEC spNewEntrance @ NewEntrance1 = '', @ NewEntrance2 = ''


Can somebody please help me? And frustrated?
Posted
Updated 21-Oct-14 7:20am
v2
Comments
Kornfeld Eliyahu Peter 21-Oct-14 13:22pm    
Use dynamic query and switch table names as required...
http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/
mikybrain1 21-Oct-14 13:29pm    
Thanks for the quick response BUT i want to be able to choose the tables dynamically.
In ur example the table (Customers) is predefined and that what i don't want.
Do u feel me?
Kornfeld Eliyahu Peter 21-Oct-14 14:02pm    
You probably didn't realized that dynamic query is in fact a string build from static and dynamic parts. Make the table name dynamic!
[no name] 21-Oct-14 14:03pm    
Being a developer involves more than just writing code. You need to develop the ability to think logically. What is preventing you from using a parameter for the table name just as demonstrated in the link that was provided to you?
Kornfeld Eliyahu Peter 21-Oct-14 14:46pm    
So a developer not only develops code but also develops self-development - I like that :-)...

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