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
 SELECT * minus a field

Author  Topic 

LeeM
Starting Member

4 Posts

Posted - 2007-07-26 : 14:20:45
Hi,

I was wondering if anyone knows a command that will select all fields in a table except one?

Thanks!
Lee

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2007-07-26 : 14:25:27
No, but if you right-click on the table and select 'Script Table As' and then 'SELECT TO', SQL will write out all the columns for you and you can delete the one you don't want. It beats typing out a bunch of column names!

Jim
Go to Top of Page

LeeM
Starting Member

4 Posts

Posted - 2007-07-26 : 14:27:26
Thank you Jim!!!
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-26 : 14:31:37
or if you drag and drop columns item in object explorer to the query window it will create a CSV of column names.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

LeeM
Starting Member

4 Posts

Posted - 2007-07-26 : 14:33:57
That sounds good too, Thanks spirit1!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-07-26 : 16:08:59
Or use the system views


-- Gen input parameter list

SELECT ', @' + Column_name + ' '+ SPACE(40-LEN(COLUMN_NAME))
+ SUBSTRING(Data_Type
+ CASE WHEN Data_type in ('decimal')
THEN '('+convert(varchar(25),Numeric_Precision)+','+convert(varchar(25),Numeric_Scale)+ ')'
WHEN Data_type in ('char','varchar')
THEN '('+convert(varchar(25),CHARACTER_MAXIMUM_LENGTH)+')'
ELSE ''
END,1,30) AS Datatype
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'Approver'
ORDER BY ORDINAL_POSITION

-- Gen OPENXML

SELECT ', [' + Column_name + '] '
+ SUBSTRING(Data_Type
+ CASE WHEN Data_type in ('decimal')
THEN '('+convert(varchar(25),Numeric_Precision)+','+convert(varchar(25),Numeric_Scale)+ ')'
WHEN Data_type in ('char','varchar')
THEN '('+convert(varchar(25),CHARACTER_MAXIMUM_LENGTH)+')'
ELSE ''
END,1,30) AS Datatype
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'StartUp'
ORDER BY ORDINAL_POSITION

--Gen XML Update
SELECT ' , a.[' + Column_name + ']= b.[' + Column_name + '] '
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'FutureRent'
ORDER BY ORDINAL_POSITION



--Generate Update

SELECT ' , ' + Column_name + SPACE(40-LEN(COLUMN_NAME)) + '= @' + Column_name + ' '
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'Approver'
ORDER BY ORDINAL_POSITION

--Generate SELECT

SELECT ' , p.[' + Column_name + ']'+ SPACE(40-LEN(COLUMN_NAME)) + ' -- ' + TABLE_NAME
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME IN ('PIF')
-- 'FutureRent'
--,'Startup'
--,'Financials'
--,'Disposition'
--,'PIF_MEP'
--,'MEPAllocation'
--,'ProjectCost'
--,'NewLease'
--,'MEP')
ORDER BY TABLE_NAME, ORDINAL_POSITION

--Generate SELECT Parameters

SELECT ' , @' + Column_name
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'MEPContact'
ORDER BY ORDINAL_POSITION

--Generate INSERT INTO

SELECT ' , [' + Column_name + ']'
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'MEPContact'
ORDER BY ORDINAL_POSITION



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

LeeM
Starting Member

4 Posts

Posted - 2007-07-27 : 09:12:47
Thanks Brett!
Go to Top of Page
   

- Advertisement -