SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get column name
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

shanmugaraj
Posting Yak Master

219 Posts

Posted - 04/17/2013 :  07:23:44  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
I have 2 tables
I want to get the column name from table 1 where the column name is not present in table 2

ie :

SELECT SUBSTRING( (SELECT ',' + name FROM sys.columns where object_id = object_id('temp_main') FOR XML PATH('')),2,200000) AS CSV

--Result--
Name,dept_id,Salary

SELECT SUBSTRING( (SELECT ',' + name FROM sys.columns where object_id = object_id('temp_dynamic') FOR XML PATH('')),2,200000) AS CSV

--Result--
Name

Expected Query to return
dept_id, Salary

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com

shanmugaraj
Posting Yak Master

219 Posts

Posted - 04/17/2013 :  07:40:05  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
could you help me on this .. i am getting error as i am trying to return as string

SELECT ',' + namecol.name from
(
SELECT name FROM sys.columns where object_id = object_id('temp_main')
EXCEPT
SELECT name FROM sys.columns where object_id = object_id('temp_dynamic')
)
namecol FOR XML PATH('')),2,200000) AS CSV
other query i tryed is

SELECT SUBSTRING(',' +
(
(SELECT name FROM sys.columns where object_id = object_id('temp_main') )
except
(SELECT name FROM sys.columns where object_id = object_id('temp_dynamic') )
)
from Table1 FOR XML PATH('')),2,200000) AS CSV


THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

satheesh
Posting Yak Master

United Kingdom
151 Posts

Posted - 04/17/2013 :  08:04:00  Show Profile  Reply with Quote
Try some thing like this,it will return unique column name.

SELECT COLUMN_NAME
FROM information_schema.columns
WHERE table_name = 'table 1' and COLUMN_NAME not in (SELECT COLUMN_NAME FROM information_schema.columns WHERE table_name = 'table 2')

Thanks
Go to Top of Page

shanmugaraj
Posting Yak Master

219 Posts

Posted - 04/17/2013 :  08:08:48  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
Thanks Satheesh !


THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 04/17/2013 :  08:38:50  Show Profile  Reply with Quote
--See the below illustration

create table abc (col1 int, col2 char(2), col3 int)
create table abc2 (col2 int, col4 char(2))

DECLARE @csv1 VARCHAR(4000) = '', @csv2 VARCHAR(4000)=''
SELECT @csv1 = @csv1 +','+Column_name
FROM (Select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='abc' 
		Except
		Select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='abc2'
	)temp
	ORDER BY COLUMN_NAME DESC;
SELECT @csv2 = @csv2 +','+ Column_name
FROM (Select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='abc2' 
		Except
		Select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='abc'
	)temp1
	
SELECT @csv1, @csv2


--
Chandu
Go to Top of Page

shanmugaraj
Posting Yak Master

219 Posts

Posted - 04/17/2013 :  09:55:42  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
Good one chandu. Thanks

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 04/18/2013 :  02:35:50  Show Profile  Reply with Quote
quote:
Originally posted by shanmugaraj

Good one chandu. Thanks
THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com


Welcome

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 04/18/2013 :  03:07:45  Show Profile  Reply with Quote
quote:
Originally posted by shanmugaraj

Good one chandu. Thanks

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com


Your initially posted solution using FOR XML should work fine and is much much efficient compared to variable concatenation method

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000