February 2008 - Posts

Different ways to know structure of a table

You can use one of the following to know the structure of a table

1 Generate SQL Script option from Enterprise Manager/Management Studio

2 select * from information_schema.columns where table_name='table_name'

3 EXEC sp_help 'table_name'

4 EXEC sp_columns 'table_name'

5 In Query Analyser type the name of the table, highlight it and press Alt+F1

Posted by Madhivanan with no comments
Filed under:

Understanding Single quotes

This is for newbies who struggle to understand how single quotes work in SQL Server

I have seen newbies worrying why the following doesnt work

SELECT columns from mytable where col ='Lifco's'

When you specify a value which has single quote, you need to double it

SELECT columns from mytable where col ='Lifco''s'

The following may be helpful (Run and see the result)

SELECT '','''','''''','''''''',''''''''''

When you use a static sql and express a value in a single quote then first and last sigle quotes specify that the value is a string. Then within those single quotes every double single quotes represent a single single quote

When you use a Dynamic sql then first and last sigle quotes specify that it is a dynamic sql. Then within those single quotes every double single quotes specify that it is a string.Then within those single quotes every four single quotes represent a single single quote

Run and see the result

EXEC('SELECT '''','''''''','''''''''''','''''''''''''''',''''''''''''''''''''')

Posted by Madhivanan with 5 comment(s)