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.
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 2ie : 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--NameExpected Query to return dept_id, SalaryTHANKSSHANMUGARAJnshanmugaraj@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 CSVother 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 THANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
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_NAMEFROM information_schema.columnsWHERE table_name = 'table 1' and COLUMN_NAME not in (SELECT COLUMN_NAME FROM information_schema.columns WHERE table_name = 'table 2')Thanks |
 |
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2013-04-17 : 08:08:48
|
Thanks Satheesh !THANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-04-17 : 08:38:50
|
--See the below illustrationcreate 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_nameFROM (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_nameFROM (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 |
 |
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2013-04-17 : 09:55:42
|
Good one chandu. ThanksTHANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-04-18 : 02:35:50
|
quote: Originally posted by shanmugaraj Good one chandu. ThanksTHANKSSHANMUGARAJnshanmugaraj@gmail.com
Welcome --Chandu |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-04-18 : 03:07:45
|
quote: Originally posted by shanmugaraj Good one chandu. ThanksTHANKSSHANMUGARAJnshanmugaraj@gmail.com
Your initially posted solution using FOR XML should work fine and is much much efficient compared to variable concatenation method------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|