Mysql生成Sqlserver创建表语句

Posted seven77yixuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql生成Sqlserver创建表语句相关的知识,希望对你有一定的参考价值。

sql:

DROP VIEW
IF EXISTS `V_TableToSqlserverText`;

CREATE ALGORITHM = UNDEFINED 
DEFINER = `root`@`localhost` 
SQL SECURITY DEFINER 
VIEW `V_TableToSqlserverText` AS (
  SELECT TABLE_NAME tablename,rownum,sqltext
	FROM(
	SELECT TABLE_NAME,0 rownum,sqltext FROM (
		SELECT c.TABLE_NAME,CONCAT(‘if object_id(‘‘‘,c.TABLE_NAME,‘‘‘,‘‘U‘‘) is not null
drop table [‘,c.TABLE_NAME,‘];
CREATE TABLE ‘,c.TABLE_NAME,‘ (‘) sqltext
		FROM information_schema.`TABLES` c 
		WHERE c.TABLE_SCHEMA=‘sevenblog‘
		AND c.TABLE_TYPE =‘BASE TABLE‘
	) startSql
	UNION ALL
	SELECT c.TABLE_NAME,c.ORDINAL_POSITION rownum,
		CONCAT(‘	‘,
			CASE
			WHEN c.ORDINAL_POSITION >1 THEN ‘,‘
			ELSE ‘‘ 
			END,
			‘[‘,
			c.COLUMN_NAME,
			‘] ‘,
			(
			CASE 
				WHEN c.COLUMN_KEY = ‘PRI‘ THEN ‘uniqueidentifier‘
				WHEN c.DATA_TYPE = ‘varchar‘ THEN CONCAT(‘n‘,c.DATA_TYPE,‘(‘,c.CHARACTER_MAXIMUM_LENGTH,‘)‘)
				ELSE c.DATA_TYPE
			END
			),
			‘ ‘,
			CASE 
			WHEN c.IS_NULLABLE = ‘NO‘ THEN ‘not null‘
			ELSE ‘null‘
			END,
			‘ ‘,
			(
			CASE 
				WHEN LENGTH(c.COLUMN_DEFAULT)>0 THEN CONCAT(‘default ‘,‘(‘,
					(
						CASE 
						WHEN c.COLUMN_DEFAULT = ‘CURRENT_TIMESTAMP‘ THEN ‘getdate()‘ 
						WHEN c.DATA_TYPE =‘varchar‘ THEN CONCAT(‘‘‘‘,c.COLUMN_DEFAULT,‘‘‘‘)
						ELSE CONCAT(c.COLUMN_DEFAULT)
						END
					),‘)‘)
				ELSE ‘‘
			END
			),
			CASE 
			WHEN c.COLUMN_KEY = ‘PRI‘ THEN ‘primary key‘
			ELSE ‘‘
			END
		) sqltext
		FROM information_schema.columns c 
		JOIN information_schema.`TABLES` b ON c.TABLE_NAME = b.TABLE_NAME
		WHERE b.TABLE_SCHEMA=‘sevenblog‘
		AND b.TABLE_TYPE =‘BASE TABLE‘
		UNION ALL
		SELECT TABLE_NAME,100 rownum,sqltext  from (
			SELECT c.TABLE_NAME,CONCAT(‘);‘) sqltext
			FROM information_schema.`TABLES` c 
			WHERE c.TABLE_SCHEMA=‘sevenblog‘
			AND c.TABLE_TYPE =‘BASE TABLE‘
		) endStr
	) tablesql
	ORDER BY tablesql.TABLE_NAME,tablesql.rownum
);

  

 结果如下

技术图片

生成sql

if object_id(‘login_log‘,‘U‘) is not null
drop table [login_log];
CREATE TABLE login_log (
	[Id] uniqueidentifier not null primary key
	,[UserID] int not null 
	,[Ipaddress] nvarchar(100) not null 
	,[Ipproxy] nvarchar(100) not null 
	,[System] nvarchar(100) not null default (‘ ‘)
	,[Option] int null 
	,[EnumDataEntityStatus] int not null default (0)
	,[CreateTime] datetime null default (getdate())
	,[LastUpdateTime] datetime null default (getdate())
	,[CreateOperator] nvarchar(50) null 
	,[LastUpdateOperator] nvarchar(50) null 
);
if object_id(‘users‘,‘U‘) is not null
drop table [users];
CREATE TABLE users (
	[Id] uniqueidentifier not null primary key
	,[Account] nvarchar(255) not null 
	,[Password] nvarchar(255) not null 
	,[Name] nvarchar(255) not null default (‘ ‘)
	,[Sex] int null 
	,[EnumDataEntityStatus] int not null default (0)
	,[BizCode] nvarchar(255) not null default (‘ ‘)
	,[TypeId] int not null default (0)
	,[TypeName] nvarchar(30) null 
	,[CreateTime] datetime null default (getdate())
	,[LastUpdateTime] datetime null default (getdate())
	,[CreateOperator] nvarchar(50) null 
	,[LastUpdateOperator] nvarchar(50) null 
);

  

 

以上是关于Mysql生成Sqlserver创建表语句的主要内容,如果未能解决你的问题,请参考以下文章

sqlserver整个数据库怎么转移到oracle数据库

怎样用sql语句在sqlserver建表 和插入数据~

sql server 2005中如何修改字段名

MS SQLServer表数据生成Insert语句

SQL如何把查询出来的多个表创建成一个临时表

sql 添加外键语句