Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

sql server - Problems with Data Script Generation

I very rarely use SQL Server and in a professional context I keep well clear! I'm working on a pet project though and I'm have problems with a script creation.

I've got an online database that I need to extract everything out of. I use the Tasks > Generate Scripts option within SQL Server Management Studio. The following is an example of one insert statement the script creates (I have 1,000s of these inserts):

INSERT [dbo].[NewComics] ([NewComicId], [Title], [Subtitle], [ReleaseDate], [CollectionId]) VALUES (366, N'Hawk & Dove 1:                                                                                      ', N'First Strikes                                                                                       ', CAST(0x00009F6F00000000 AS DateTime), 248)

I have two issues with this:

(a) I want to strip all the whitespace from the two title elements (b) I don't want a HEX date - I want something readable like 2006-09-01 (yyyy-mm-dd)

INSERT [dbo].[NewComics] ([NewComicId], [Title], [Subtitle], [ReleaseDate], [CollectionId]) VALUES (366, N'Hawk & Dove 1:', N'First Strikes', '2006-09-01', 248)

What would be the quickest way to change about 3,000 insert statements to this revised format?

FYI - this is the design of the table:

[NewComicId] [int] NOT NULL,
[Title] [nchar](100) NOT NULL,
[Subtitle] [nchar](100) NULL,
[ReleaseDate] [datetime] NOT NULL,
[CollectionId] [int] NOT NULL,

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, generate scripts sadly scripts datetime columns as CONVERT(binary_value, Datetime). I'll try to get an answer as to why (or more importantly if there is a way to change the behavior). I suspect the reason is to avoid any issues with running the scripts on a different machine with different locale / regional settings etc. I don't know if there's a way to change that from happening in the meantime, but Management Studio isn't the only way to script your data... you could look into 3rd party products like Red-Gate's SQL Data Compare.

If it's really only 3,000 rows, and you intend to run the generated script on a different server, stop using the wizard and do this (on first glance this looks horrific, but it does several of the things you'll want - outputs a script ready to copy, paste and run, with nicely formatted and readable dates, inserts batched into multi-row VALUES by 1000 with GO commands in between, and even deals with potentially NULL values in title, subtitle and collectionid):

DECLARE @newtable SYSNAME = 'dbo.NewComics';

SET NOCOUNT ON;

;WITH x AS (SELECT TOP (4000) s = '(' 
    + CONVERT(VARCHAR(12), NewComicId) + ','
    + COALESCE('N''' + REPLACE(RTRIM(Title), '''', '''''') + '''', 'NULL') + ',' 
    + COALESCE('N''' + REPLACE(RTRIM(SubTitle), '''', '''''') + '''', 'NULL') 
    + ', ''' + CONVERT(CHAR(8), ReleaseDate, 112) + ' '
    + CONVERT(CHAR(8), ReleaseDate, 108) + ''','
    + CONVERT(VARCHAR(12), COALESCE(CollectionId, 'NULL')) + ')',
  rn = ROW_NUMBER() OVER (ORDER BY NewComicId)
  FROM dbo.OldComics ORDER BY NewComicId
),
y AS
(
SELECT [/*a*/] = 1, [/*b*/] = 'SET NOCOUNT ON;
GO
INSERT ' + @newtable + ' VALUES'
UNION ALL 
SELECT 2, s = CASE WHEN rn > 1 THEN ',' ELSE '' END + s
 FROM x WHERE rn BETWEEN 1 AND 1000
UNION ALL 
SELECT 3, 'GO' UNION ALL 
SELECT 4, s = CASE WHEN rn > 1001 THEN ',' ELSE '' END + s
 FROM x WHERE rn BETWEEN 1001 AND 2000
UNION ALL 
SELECT 5, 'GO' UNION ALL 
SELECT 6, s = CASE WHEN rn > 2001 THEN ',' ELSE '' END + s
 FROM x WHERE rn BETWEEN 2001 AND 3000
UNION ALL 
SELECT 7, 'GO' UNION ALL 
SELECT 8, s = CASE WHEN rn > 3001 THEN ',' ELSE '' END + s
 FROM x WHERE rn BETWEEN 3001 AND 4000
)
SELECT [/*b*/] FROM y ORDER BY [/*a*/];

(You might have to play with it if you have exactly 3000 or 3001 rows, or add another couple of unions if you have more than 4000, etc.)

If you are moving the data to a different table or different database on the same instance, use the script that @swasheck provided (and again, stop using the wizard).

You may have noticed a common trend here: stop using the generate scripts wizard if you don't like the binary format it outputs for dates.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...