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
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Need help sql select query

Author  Topic 

murlock1988
Starting Member

1 Post

Posted - 2009-08-16 : 06:57:30
Hi

I'm currently doing a project using C#.net and sql server 2005, and I've run into a problem with a select statement! This select statment has to select all column names of a certain table (this is specified in the variable tablename) but i dont want it to include the column names of type int. Please could you help me with this? Here is the select statement i'm currently trying to us but get the error: "System.Data.SqlClient.SqlException: Invalid column name 'DATA_TYPE'."

cmd.CommandText = "SELECT [name] AS [Column name] FROM syscolumns WHERE id = (SELECT id  FROM sysobjects WHERE type = 'U' AND [NAME] = '" + tablename + "') AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')";


Thanks in advance!!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-16 : 08:23:35
[code]cmd.CommandText = "SELECT COLUMN_NAME AS [Column name]
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE LIKE '%CHAR'
AND OBJECTPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), 'IsTable') = 1
AND TABLE_NAME = '" + TableName + "'"[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -