Scott on Writing

Musings on technical writing...

Neat Little SQL Server 2000 Enterprise Manager Tip

I've been using SQL Server Enterprise Manager since 1998, but just today realized that you could, from the Tables listing, hit Ctrl+C on a table name and then go to a text editor and hit Ctrl+V and get the appropriate CREATE TABLE SQL syntax for the “copied” table (which includes constraints).  Prior to this, when I've needed a table's T-SQL definition I've always right-clicked on the table name, gone to All Tasks and then selected Generate SQL Scripts.  This latter approach affords more options (such as scripting multiple database entities, scripting dependent objects, adding DROP statements, etc.), but the simple “copy-paste” technique is a fast way to just grab the create script for a single table.  (This handy shortcut works for stored procedures, too!)

On an aside, I noticed today that my blog, since its inception, has had over 1,000 comments from readers.  (And I'm very certain those are high signal to noise ratio posts, since I am a bit pedantic about deleting/preventing blog comment spam.)  A hearty thank you to those who have helped improve the content on my blog by adding your own commentary, anecdotes, experiences, and suggestions.  Keep 'em coming!

posted on Friday, December 10, 2004 12:33 PM

Feedback

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 12/10/2004 1:49 PM Scott

See, I blew away my Windows profile at some point and, for some reason, I lost my SQL EM MMC plugin. I re-added it to the SQL EM but I had to save it as an .msc file. To make a confusing story short, I end up using Query Analyzer to do most of my DB work.

You can browse the DB objects in the left hand pane, right click on them, select "script to new window as...", and select from a variety of scripting options "Create", "Alter" , "Execute", etc... Depending on the type of DB object you click on.

It's helped me remember my T-SQL syntax and ends up being quicker in the long run than clicking around in SQL EM.

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 12/10/2004 2:32 PM Scott Elkin

That is like the greatest tip ever. I just love little things like that!

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 12/10/2004 3:20 PM David

You can drag & drop your table into SQL Query Analyzer as well, it will do the same.

# Neat Little SQL Server 2000 Enterprise Manager Tip (from: Scott Mitchell) 12/10/2004 8:42 PM TOURNEY LOGIC LINK BLOG

# Copy/Paste DDL From SQL Server Ent Mgr 12/12/2004 1:21 AM protected virtual void jayBlog {

# SQL Enterprise Manager Copy-paste tip from Scott Mitchell 12/13/2004 7:22 AM Dave Burke's Blog

# SQL Enterprise Manager Copy-paste tip from Scott Mitchell 12/13/2004 7:23 AM Dave Burke's Blog

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 12/13/2004 10:31 AM Aaron Meis

You can also select many objects (tables, etC), but beware the resource meter! Might take a few minutes, but you can copy many items at one time. :)Thanks for the tip Scott.

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 12/15/2004 8:27 AM Hilton Giesenow

Kewl tip! It works for other objects as well (SPs, etc.). Thanks for this.

# SQL Server 2000 Enterprise Manager Tip 12/19/2004 6:35 PM Patrick Steele's .NET Blog

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 12/19/2004 7:24 PM foobar

I love you. You have made my week. Seriously. This is one of the greatest time savers ever.

# scripting the datas 12/19/2004 11:33 PM kerberoz

what about making a script of all the datas of the table? how we can do that easily?

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 12/20/2004 8:54 AM Scott Mitchell

kerberoz, you can use the General SQL Script task in SQL Enterprise Manager to script all tables, sprocs, views, UDFs, etc. In SQL Server 2000, go to the Tools menu and there's an option titled "Generate SQL Script..."

hth!

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 12/26/2004 1:46 AM kerberoz

thanks scott for the reply, but what i meant for my post is that scripting the actual data, i.e. a script with a several insert into tablename(...)

TIA!

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 12/31/2004 7:31 AM Scott Mitchell

kerberoz, there are tools that provide the functionality that you need. For example, see Red-Gate software's SQL tools - http://www.red-gate.com/sql/summary.htm

hth

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 1/5/2005 3:02 AM manish

Good I also discovered this option while working gon one of my projects. Its cool !! Anyways I have even experienced the transfer of MS Access tables to SQL Server easily. Can you post something about indexes in detail .

Regards,
manish

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 5/5/2005 6:06 AM Brian Boyce

kerberoz
Here is a stored proc that should give you a starter.

Stored Procedure to Create Insert commands from table of data
At the moment dates need to be edited.

CREATE PROCEDURE usp_CreateInsert (@objname sysname, @includeID bit)
AS
-- Currently the proc ignores Identity columns (by checking syscolumns.status & 128 (0x80)
SET NOCOUNT ON

DECLARE @first bit,
@objid int,
@colid int,
@quote varchar(4),
@colname sysname,
@txt varchar(250),
@select varchar(8000),
@sqltxt varchar(8000)

SET @quote = ''''

SELECT @objid = id
FROM sysobjects
WHERE name = @objname

CREATE TABLE #sphelptab
(
col_name char (30) NULL,
col_type char (30) NULL,
col_type_val int NULL,
col_len int NULL,
col_prec char (5) NULL,
col_scale char (5) NULL,
col_status int NULL,
colid int NULL
)
INSERT
INTO #sphelptab
SELECT c.name,
t.name,
c.type,
c.length,
CONVERT(CHAR(5),c.prec),
CONVERT(CHAR(5),c.scale),
c.status,
c.colid
FROM syscolumns c,
systypes t
WHERE c.id = @objid
AND c.usertype *= t.usertype

--
-- Don't display precision and scale for datatypes
-- which they not applicable.
--

UPDATE #sphelptab
SET col_prec = '',
col_scale = ''
WHERE col_type IN
(SELECT name FROM systypes WHERE type NOT IN
(38,48,52,55,56,59,60,62,63,106,108,109,110,122))
--
-- Insert script
--
SELECT @sqltxt = 'INSERT [' + @objname + '] ('

if (@includeID = 0) begin
SELECT @colid = MIN(colid),
@first = 1
FROM #sphelptab
WHERE col_status & 128 <> 128
end
else begin
SELECT @colid = MIN(colid),
@first = 1
FROM #sphelptab
end

SET @select = 'SELECT '
WHILE (@colid IS NOT NULL) BEGIN

SELECT @colname= RTRIM(col_name),
@txt = CASE
WHEN col_type_val IN (37,39,47) THEN
' ISNULL(' + @quote+@quote+@quote+@quote +'+'+ RTRIM(col_name) +'+'+

@quote+@quote+@quote+@quote + ',''NULL'')'
ELSE ' ISNULL(CAST('+RTRIM(col_name)+ ' AS VARCHAR),''NULL'')'
END
FROM #sphelptab
WHERE colid = @colid

IF @first = 0
SELECT @sqltxt = @sqltxt + ', ' + @colname,
@select = @select + '+' + @quote + ',' + @quote + '+' + @txt
ELSE
SELECT @sqltxt = @sqltxt + ' ' + @colname,
@select = @txt

SELECT @colid = MIN(colid),
@first = 0
FROM #sphelptab
WHERE col_status & 128 <> 128
AND colid > @colid

END

SELECT @sqltxt = @sqltxt + ') VALUES (' + @quote + '+',
@select = @select + '+' + @quote + ')' + @quote + ' FROM [' + @objname + ']'

if (@includeID = 1) begin
select 'SET IDENTITY_INSERT [' + @objname + '] ON'
select 'GO'
end

EXEC( 'SELECT ' + @quote + @sqltxt + @select)

if (@includeID = 1) begin
select 'SET IDENTITY_INSERT [' + @objname + '] OFF'
select 'GO'
end

GO


# re: Neat Little SQL Server 2000 Enterprise Manager Tip 7/6/2005 11:55 AM neonprimetime

Excellent tip ... found it with a google search, and it really helped ... ty

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 7/7/2005 7:55 AM Madhu


This is a fantastic tip.

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 7/25/2005 6:44 PM Joe Rocker

Awesome tip. It wasn't what I was looking for, but still way cool.

# re: Neat Little SQL Server 2000 Enterprise Manager Tip 1/24/2006 2:37 AM Ari

Not very impressed by this functionality :) I would rather that Enterprise manager acted more like Windows explorer, i.e. copy/paste of a table would create a new table named "copy of..." containing the same data and constraints as the table being copied.

Thanks for your blog!

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles