Tool for converting all VARCHAR to NVARCHAR in one or few steps
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I am using SQL Server Management Studio and need to convert all VARCHAR columns to NVARCHAR. Is there a tool or something similar to do the conversion in one or few steps? It should be applied to different databases.
Addition:
Excuse me, but is it possible that you gave me a script to convert NVARCHAR to VARCHAR? I need it the other way round. Also when I run the script which was produced by your script it gave me the error
Meldung 102, Ebene 15, Status 1, Zeile 1
Falsche Syntax in der Nähe von 'GO'.
multiple times. Actually I thought that the solution your script produce, to simply alter the tables doesnt work when there are constraints related to tables. Thats why I asked whether there is a tool that can do this, because I thought a simple script has restrictions.
sql-server t-sql varchar
add a comment |
I am using SQL Server Management Studio and need to convert all VARCHAR columns to NVARCHAR. Is there a tool or something similar to do the conversion in one or few steps? It should be applied to different databases.
Addition:
Excuse me, but is it possible that you gave me a script to convert NVARCHAR to VARCHAR? I need it the other way round. Also when I run the script which was produced by your script it gave me the error
Meldung 102, Ebene 15, Status 1, Zeile 1
Falsche Syntax in der Nähe von 'GO'.
multiple times. Actually I thought that the solution your script produce, to simply alter the tables doesnt work when there are constraints related to tables. Thats why I asked whether there is a tool that can do this, because I thought a simple script has restrictions.
sql-server t-sql varchar
add a comment |
I am using SQL Server Management Studio and need to convert all VARCHAR columns to NVARCHAR. Is there a tool or something similar to do the conversion in one or few steps? It should be applied to different databases.
Addition:
Excuse me, but is it possible that you gave me a script to convert NVARCHAR to VARCHAR? I need it the other way round. Also when I run the script which was produced by your script it gave me the error
Meldung 102, Ebene 15, Status 1, Zeile 1
Falsche Syntax in der Nähe von 'GO'.
multiple times. Actually I thought that the solution your script produce, to simply alter the tables doesnt work when there are constraints related to tables. Thats why I asked whether there is a tool that can do this, because I thought a simple script has restrictions.
sql-server t-sql varchar
I am using SQL Server Management Studio and need to convert all VARCHAR columns to NVARCHAR. Is there a tool or something similar to do the conversion in one or few steps? It should be applied to different databases.
Addition:
Excuse me, but is it possible that you gave me a script to convert NVARCHAR to VARCHAR? I need it the other way round. Also when I run the script which was produced by your script it gave me the error
Meldung 102, Ebene 15, Status 1, Zeile 1
Falsche Syntax in der Nähe von 'GO'.
multiple times. Actually I thought that the solution your script produce, to simply alter the tables doesnt work when there are constraints related to tables. Thats why I asked whether there is a tool that can do this, because I thought a simple script has restrictions.
sql-server t-sql varchar
sql-server t-sql varchar
edited Dec 14 '16 at 14:46
Eduard Ge
asked Dec 12 '16 at 12:32
Eduard GeEduard Ge
63
63
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
no tool needed just some good old TSQL code.
This will generate all the statements.
DECLARE @Statements table (Statement varchar(max))
DECLARE @CMD varchar(max)
DECLARE @DB varchar(500)
DECLARE c CURSOR for select distinct name from sys.databases where database_id > 4
OPEN C
FETCH NEXT FROM C into @DB
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CMD = ' USE '+quotename(@DB)+';
select ''USE '+quotename(@DB)+' GO
ALTER TABLE ''+QUOTENAME(s.name)+''.''+QUOTENAME(t.name)+''
ALTER COLUMN ''+quotename(c.name)+'' varchar(''+cast(c.max_length as varchar)+'')
GO'' from '+QUOTENAME(@DB)+'.sys.tables t
inner join '+QUOTENAME(@DB)+'.sys.schemas s on s.schema_id = t.schema_id
inner join '+QUOTENAME(@DB)+'.sys.columns c on c.object_id = t.object_id
where is_ms_shipped = 0
and system_type_id = 231
'
INSERT INTO @Statements
EXEC (@CMD)
FETCH NEXT FROM C into @DB
END
CLOSE C
DEALLOCaTE C
SELECT * from @Statements
You could use sp_MSforeachdb instead of a cursor too. mssqltips.com/sqlservertip/1414/…
– Jorriss
Dec 12 '16 at 14:06
Indeed, but that is undocumented and sometimes gives some weird results/errors :)
– Stijn Wynants
Dec 12 '16 at 14:08
1
But so much cleaner. Don't you wish they just unleashed the hounds on that one?
– Jorriss
Dec 12 '16 at 14:37
Indeed! But I've noticed that putting my faith in msforeachdb can backfire :) have not had that yet with the DB cursor.
– Stijn Wynants
Dec 12 '16 at 14:39
add a comment |
what about varchar(max)? that will give an error using cast(c.max_length as varchar)....
New contributor
user3365476 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f157890%2ftool-for-converting-all-varchar-to-nvarchar-in-one-or-few-steps%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
no tool needed just some good old TSQL code.
This will generate all the statements.
DECLARE @Statements table (Statement varchar(max))
DECLARE @CMD varchar(max)
DECLARE @DB varchar(500)
DECLARE c CURSOR for select distinct name from sys.databases where database_id > 4
OPEN C
FETCH NEXT FROM C into @DB
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CMD = ' USE '+quotename(@DB)+';
select ''USE '+quotename(@DB)+' GO
ALTER TABLE ''+QUOTENAME(s.name)+''.''+QUOTENAME(t.name)+''
ALTER COLUMN ''+quotename(c.name)+'' varchar(''+cast(c.max_length as varchar)+'')
GO'' from '+QUOTENAME(@DB)+'.sys.tables t
inner join '+QUOTENAME(@DB)+'.sys.schemas s on s.schema_id = t.schema_id
inner join '+QUOTENAME(@DB)+'.sys.columns c on c.object_id = t.object_id
where is_ms_shipped = 0
and system_type_id = 231
'
INSERT INTO @Statements
EXEC (@CMD)
FETCH NEXT FROM C into @DB
END
CLOSE C
DEALLOCaTE C
SELECT * from @Statements
You could use sp_MSforeachdb instead of a cursor too. mssqltips.com/sqlservertip/1414/…
– Jorriss
Dec 12 '16 at 14:06
Indeed, but that is undocumented and sometimes gives some weird results/errors :)
– Stijn Wynants
Dec 12 '16 at 14:08
1
But so much cleaner. Don't you wish they just unleashed the hounds on that one?
– Jorriss
Dec 12 '16 at 14:37
Indeed! But I've noticed that putting my faith in msforeachdb can backfire :) have not had that yet with the DB cursor.
– Stijn Wynants
Dec 12 '16 at 14:39
add a comment |
no tool needed just some good old TSQL code.
This will generate all the statements.
DECLARE @Statements table (Statement varchar(max))
DECLARE @CMD varchar(max)
DECLARE @DB varchar(500)
DECLARE c CURSOR for select distinct name from sys.databases where database_id > 4
OPEN C
FETCH NEXT FROM C into @DB
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CMD = ' USE '+quotename(@DB)+';
select ''USE '+quotename(@DB)+' GO
ALTER TABLE ''+QUOTENAME(s.name)+''.''+QUOTENAME(t.name)+''
ALTER COLUMN ''+quotename(c.name)+'' varchar(''+cast(c.max_length as varchar)+'')
GO'' from '+QUOTENAME(@DB)+'.sys.tables t
inner join '+QUOTENAME(@DB)+'.sys.schemas s on s.schema_id = t.schema_id
inner join '+QUOTENAME(@DB)+'.sys.columns c on c.object_id = t.object_id
where is_ms_shipped = 0
and system_type_id = 231
'
INSERT INTO @Statements
EXEC (@CMD)
FETCH NEXT FROM C into @DB
END
CLOSE C
DEALLOCaTE C
SELECT * from @Statements
You could use sp_MSforeachdb instead of a cursor too. mssqltips.com/sqlservertip/1414/…
– Jorriss
Dec 12 '16 at 14:06
Indeed, but that is undocumented and sometimes gives some weird results/errors :)
– Stijn Wynants
Dec 12 '16 at 14:08
1
But so much cleaner. Don't you wish they just unleashed the hounds on that one?
– Jorriss
Dec 12 '16 at 14:37
Indeed! But I've noticed that putting my faith in msforeachdb can backfire :) have not had that yet with the DB cursor.
– Stijn Wynants
Dec 12 '16 at 14:39
add a comment |
no tool needed just some good old TSQL code.
This will generate all the statements.
DECLARE @Statements table (Statement varchar(max))
DECLARE @CMD varchar(max)
DECLARE @DB varchar(500)
DECLARE c CURSOR for select distinct name from sys.databases where database_id > 4
OPEN C
FETCH NEXT FROM C into @DB
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CMD = ' USE '+quotename(@DB)+';
select ''USE '+quotename(@DB)+' GO
ALTER TABLE ''+QUOTENAME(s.name)+''.''+QUOTENAME(t.name)+''
ALTER COLUMN ''+quotename(c.name)+'' varchar(''+cast(c.max_length as varchar)+'')
GO'' from '+QUOTENAME(@DB)+'.sys.tables t
inner join '+QUOTENAME(@DB)+'.sys.schemas s on s.schema_id = t.schema_id
inner join '+QUOTENAME(@DB)+'.sys.columns c on c.object_id = t.object_id
where is_ms_shipped = 0
and system_type_id = 231
'
INSERT INTO @Statements
EXEC (@CMD)
FETCH NEXT FROM C into @DB
END
CLOSE C
DEALLOCaTE C
SELECT * from @Statements
no tool needed just some good old TSQL code.
This will generate all the statements.
DECLARE @Statements table (Statement varchar(max))
DECLARE @CMD varchar(max)
DECLARE @DB varchar(500)
DECLARE c CURSOR for select distinct name from sys.databases where database_id > 4
OPEN C
FETCH NEXT FROM C into @DB
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CMD = ' USE '+quotename(@DB)+';
select ''USE '+quotename(@DB)+' GO
ALTER TABLE ''+QUOTENAME(s.name)+''.''+QUOTENAME(t.name)+''
ALTER COLUMN ''+quotename(c.name)+'' varchar(''+cast(c.max_length as varchar)+'')
GO'' from '+QUOTENAME(@DB)+'.sys.tables t
inner join '+QUOTENAME(@DB)+'.sys.schemas s on s.schema_id = t.schema_id
inner join '+QUOTENAME(@DB)+'.sys.columns c on c.object_id = t.object_id
where is_ms_shipped = 0
and system_type_id = 231
'
INSERT INTO @Statements
EXEC (@CMD)
FETCH NEXT FROM C into @DB
END
CLOSE C
DEALLOCaTE C
SELECT * from @Statements
edited Dec 12 '16 at 14:07
answered Dec 12 '16 at 14:02
Stijn WynantsStijn Wynants
1,506715
1,506715
You could use sp_MSforeachdb instead of a cursor too. mssqltips.com/sqlservertip/1414/…
– Jorriss
Dec 12 '16 at 14:06
Indeed, but that is undocumented and sometimes gives some weird results/errors :)
– Stijn Wynants
Dec 12 '16 at 14:08
1
But so much cleaner. Don't you wish they just unleashed the hounds on that one?
– Jorriss
Dec 12 '16 at 14:37
Indeed! But I've noticed that putting my faith in msforeachdb can backfire :) have not had that yet with the DB cursor.
– Stijn Wynants
Dec 12 '16 at 14:39
add a comment |
You could use sp_MSforeachdb instead of a cursor too. mssqltips.com/sqlservertip/1414/…
– Jorriss
Dec 12 '16 at 14:06
Indeed, but that is undocumented and sometimes gives some weird results/errors :)
– Stijn Wynants
Dec 12 '16 at 14:08
1
But so much cleaner. Don't you wish they just unleashed the hounds on that one?
– Jorriss
Dec 12 '16 at 14:37
Indeed! But I've noticed that putting my faith in msforeachdb can backfire :) have not had that yet with the DB cursor.
– Stijn Wynants
Dec 12 '16 at 14:39
You could use sp_MSforeachdb instead of a cursor too. mssqltips.com/sqlservertip/1414/…
– Jorriss
Dec 12 '16 at 14:06
You could use sp_MSforeachdb instead of a cursor too. mssqltips.com/sqlservertip/1414/…
– Jorriss
Dec 12 '16 at 14:06
Indeed, but that is undocumented and sometimes gives some weird results/errors :)
– Stijn Wynants
Dec 12 '16 at 14:08
Indeed, but that is undocumented and sometimes gives some weird results/errors :)
– Stijn Wynants
Dec 12 '16 at 14:08
1
1
But so much cleaner. Don't you wish they just unleashed the hounds on that one?
– Jorriss
Dec 12 '16 at 14:37
But so much cleaner. Don't you wish they just unleashed the hounds on that one?
– Jorriss
Dec 12 '16 at 14:37
Indeed! But I've noticed that putting my faith in msforeachdb can backfire :) have not had that yet with the DB cursor.
– Stijn Wynants
Dec 12 '16 at 14:39
Indeed! But I've noticed that putting my faith in msforeachdb can backfire :) have not had that yet with the DB cursor.
– Stijn Wynants
Dec 12 '16 at 14:39
add a comment |
what about varchar(max)? that will give an error using cast(c.max_length as varchar)....
New contributor
user3365476 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
what about varchar(max)? that will give an error using cast(c.max_length as varchar)....
New contributor
user3365476 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
what about varchar(max)? that will give an error using cast(c.max_length as varchar)....
New contributor
user3365476 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
what about varchar(max)? that will give an error using cast(c.max_length as varchar)....
New contributor
user3365476 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user3365476 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 13 mins ago
user3365476user3365476
1
1
New contributor
user3365476 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user3365476 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
user3365476 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f157890%2ftool-for-converting-all-varchar-to-nvarchar-in-one-or-few-steps%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown