Batch converts the char in all the table and table in a library to nchar type

Source: Internet
Author: User
Tags join
/*--to change the numeric type from Char,varchar to Nchar,nvarchar stored procedures in all tables--*/

/*--Call Example:
EXEC P_set
--*/
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ P_set] and OBJECTPROPERTY (ID, N ' isprocedure ') = 1)
drop procedure [dbo]. [P_set]
Go

CREATE PROCEDURE P_set
As
DECLARE TB cursor FOR
SELECT sql= ' ALTER TABLE [' +d.name
+ '] Alter column [' +a.name+ '] n '
+b.name+ ' (' +cast (a.length*2 as varchar) + ') '
From Syscolumns A
Left join Systypes B on A.xtype=b.xusertype
INNER JOIN sysobjects D on a.id=d.id and d.xtype= ' U ' and d.name<> ' dtproperties '
where
B.name in (' char ', ' varchar ')
and
Not EXISTS (SELECT 1 from sysobjects where xtype= ' PK ' and name in (
SELECT name from sysindexes WHERE indid in (
SELECT indid from Sysindexkeys WHERE id = a.id and colid=a.colid
))--primary key cannot be modified
ORDER BY D.name,a.name

DECLARE @sql varchar (1000)
Open TB
FETCH NEXT from TB into @sql
While @ @fetch_status = 0
Begin
EXEC (@sql)
FETCH NEXT from TB into @sql
End
Close TB
Deallocate TB
Go


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.