I determinate the data type of a physical table field

A

ad

I want to change a field a SQLServer table to varchar(10) if it is
nvarchar(5).

How can I determinate the data type of a physical table field in SQLServer
2005?
 
D

Dave Sexton

Hi,

Issue a query using INFORMATION_SCHEMA.COLUMNS. For example, to get a
DataSet containing the column information of a table named, "YourTable" that
meet your specific requirements:

DataSet data = new DataSet();

using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(
@"SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'YourTable'
AND DATA_TYPE = N'nvarchar'
AND CHARACTER_MAXIMUM_LENGTH = 5", conn))
{
adapter.Fill(data);
}

// TODO: alter table
}

You can issue an ALTER TABLE T-SQL statement to perform the modification.

An alternative to this is SMO:

"Sql Server Management Objects"
http://msdn2.microsoft.com/en-us/library/ms162169.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top