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
 Table Scripts

Author  Topic 

sankar.c.2006@gmail.com
Starting Member

2 Posts

Posted - 2009-04-03 : 03:27:42
Hi

I want to see the table scripts using SQL QUERY. IS IT POSSIBLE ?Can anyone help me plz?

Regards
Sankar

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-04-03 : 04:44:39
Here is the procedure to do this...

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/generate-sql-script.aspx

Take a Look At this...
Thanks.

Go to Top of Page

sankar.c.2006@gmail.com
Starting Member

2 Posts

Posted - 2009-04-03 : 05:43:17
quote:
Originally posted by ashishashish

Here is the procedure to do this...

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/generate-sql-script.aspx

Take a Look At this...
Thanks.







I need only in SQL Statement.. Not in Procedure.....................
Go to Top of Page

darkdusky
Aged Yak Warrior

591 Posts

Posted - 2009-04-03 : 09:51:49
You can use the code inside Madhivanan's procedure but just remove the first few lines and replace with:
declare @tableName varchar(100)
set @tableName ='xxxx' --Put your tablename here
If exists (Select * from Information_Schema.COLUMNS where Table_Name= @tableName)


Or else

you can generate script for every table in database with:


DECLARE @Table_Definition VARCHAR(MAX),

@TableName VARCHAR(25)


DECLARE TableCursor CURSOR FOR

select table_catalog + '.' + table_schema + '.' + table_name as TableName
from information_schema.tables
where table_type='base table'


OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Table_Definition = 'IF NOT EXISTS(SELECT 1 FROM sys.tables WHERE name = ' +

QUOTENAME(@TableName,'''') + ')' + CHAR(13) + 'BEGIN'

SET @Table_Definition = COALESCE(@Table_Definition + CHAR(13),'') + 'CREATE TABLE('



SELECT @Table_Definition = COALESCE(@Table_Definition,'') +

CHAR(13) + Column_Name + SPACE(1) + UPPER(Data_Type) +

CASE

WHEN data_type IN ('REAL','money','DECIMAL','NUMERIC') THEN

'(' + CONVERT(VARCHAR(3),COALESCE(numeric_precision,'')) + ','+

CONVERT(VARCHAR(3),COALESCE(Numeric_Scale,'')) + ')'

WHEN data_type IN ('CHAR','nvarchar','VARCHAR','NCHAR') THEN

'(' + CAST(isnull(Character_Maximum_Length,'') AS VARCHAR) + ')'

ELSE '' END +

CASE WHEN Is_Nullable='NO' THEN ' NOT NULL,' ELSE ' NULL,' END

FROM Information_Schema.Columns

WHERE Table_Name = @TableName



SET @Table_Definition = STUFF(@Table_Definition,LEN(@Table_Definition),1,CHAR(13) + ')' + CHAR(13) + 'END')



PRINT @Table_Definition

--EXEC (@Table_Definition)
FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor

DEALLOCATE TableCursor
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-04-04 : 04:37:25
Hey Thats kk....
if i want to script all the store procedure in my database created by user...

Thanks...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-04-04 : 06:18:52
quote:
Originally posted by ashishashish

Hey Thats kk....
if i want to script all the store procedure in my database created by user...

Thanks...


Refer http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/13/script-out-procedures-and-functions-part-2.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-04-04 : 07:43:39
Many Many Thanks to u......
m just out of my Mind because i get all the values in table but i cant think of that we can also export it through BCP in to .txt file..
Many Many Thanks...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-04-04 : 08:27:53
quote:
Originally posted by ashishashish

Many Many Thanks to u......
m just out of my Mind because i get all the values in table but i cant think of that we can also export it through BCP in to .txt file..
Many Many Thanks...


You are welcome

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -