Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get column name

Author  Topic 

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2013-04-17 : 07:23:44
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 - 2013-04-17 : 07:40:05
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

152 Posts

Posted - 2013-04-17 : 08:04:00
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 - 2013-04-17 : 08:08:48
Thanks Satheesh !


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

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-17 : 08:38:50
--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 - 2013-04-17 : 09:55:42
Good one chandu. Thanks

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

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-18 : 02:35:50
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

52326 Posts

Posted - 2013-04-18 : 03:07:45
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
   

- Advertisement -