求教sqlserver通过命令行生成建表语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求教sqlserver通过命令行生成建表语句相关的知识,希望对你有一定的参考价值。
比如通过以下语句能返回存储过程内容
EXEC sp_helptext '存储过程名称'
有没有类似的语句,能够返回create table语句?
set @tablename ='test'--这里输入表名
set @sql = 'create table ['+@tablename+']
(
'
select @sql = @sql + b.name + ' '+
c.name+
case when c.collation_name is not null then '('+
case when b.max_length <>-1 then convert(varchar(100),b.max_length)
else 'MAX'
end +') '
else ''
end +
case when b.is_identity = 1 then ' identity('+convert(varchar(100),IDENT_SEED(@tablename))+','+convert(varchar(100),IDENT_INCR(@tablename))+')' else '' end +
case when d.definition is not null then ' default('+d.definition +')' else '' end +
case when b.is_nullable = 0 then ' not null' else ' null' end +
',
'
from sys.objects a join sys.columns b
on a.object_id = b.object_id
join sys.types c
on b.system_type_id = c.system_type_id and b.user_type_id = c.user_type_id
left join sys.default_constraints d
on b.default_object_id = d.object_id
where a.name=@tablename
order by b.column_id
if exists(select * from sys.indexes where object_id =object_id(@tablename) and is_primary_key =1 )
begin
select @sql = @sql + 'CONSTRAINT ['+name+'] PRIMARY KEY '+type_desc+'
(
' from sys.indexes where object_id =object_id(@tablename) and is_primary_key =1
select @sql = @sql + b.name + case when a.is_descending_key =1 then ' DESC' else ' ASC' end +',
' from sys.index_columns a join sys.columns b
on a.object_id= b.object_id and a.column_id = b.column_id
where a.object_id =object_id(@tablename)
select @sql = left(@sql,len(@sql)-3)+'
)'
select @sql = @sql+'
) ON [PRIMARY] '
end
else
begin
select @sql = left(@sql,len(@sql)-1)+'
) ON [PRIMARY] '
end
print @sql 参考技术A 自己下载一个sqlserver客户端 里面有各种各样的导出方式 ,导出表 导出记录都可以追问
我知道客户端通过鼠标点一点,是能手工导出语句。
但我目的是通过编程的方式获取数据库里所有表的建表语句。
Excel生成建表角本
使用宏生成建表角本:
Private Sub CreateFile() Dim Fcreate As Object \'建表头 Dim FSO As Object Dim Fopen As Object \'追加语句 Dim Dir As Object \'建目录 Dim Fcreate_run As Object \'总调角本 Set mysheet1 = Workbooks(ThisWorkbook.Name).Sheets(1) \'Sheets1 Set mysheet = Workbooks(ThisWorkbook.Name).Sheets(2) \'Sheets2 Set FSO = CreateObject("Scripting.FileSystemObject") \'建目录 If FSO.FolderExists(ThisWorkbook.Path & "\\DB") = False Then \'判断文件夹是否存在 FSO.CreateFolder (ThisWorkbook.Path & "\\DB") FSO.CreateFolder (ThisWorkbook.Path & "\\DB\\DBS") FSO.CreateFolder (ThisWorkbook.Path & "\\DB\\DBS\\" & mysheet1.Range("A2").Value & "") FSO.CreateFolder (ThisWorkbook.Path & "\\DB\\DBS\\" & mysheet1.Range("A2").Value & "\\Tables") FSO.CreateFolder (ThisWorkbook.Path & "\\DB\\PATCH") Set Fcreate_run = FSO.CreateTextFile(ThisWorkbook.Path & "\\DB\\PATCH\\run.sql", True) \'建总调角本 Fcreate_run.WriteLine ("-- Create Tables ") Fcreate_run.Close Set Fcreate_run = Nothing End If \'建表块 For i = 2 To mysheet.UsedRange.Rows.Count \'遍历所有的列 If mysheet.Range("H" & i).Value = mysheet1.Range("B2").Value Then \'判断版本是否一致 If FSO.FileExists(ThisWorkbook.Path & "\\DB\\DBS\\" & mysheet1.Range("A2").Value & "\\Tables\\" & mysheet.Range("A" & i).Value & ".tab") Then \'判断SQL文件是否存在 Set Fopen = FSO.OpenTextFile(ThisWorkbook.Path & "\\DB\\DBS\\" & mysheet1.Range("A2").Value & "\\Tables\\" & mysheet.Range("A" & i).Value & ".tab", 8, False) Fopen.Write (" ," & mysheet.Range("C" & i).Value & " " & mysheet.Range("E" & i).Value) If mysheet.Range("G" & i).Value = "Y" Then \'非空处理 Fopen.WriteLine (" not null") Else Fopen.WriteLine ("") End If If mysheet.Range("A" & i).Value <> mysheet.Range("A" & i + 1).Value Then \'字段结束判断 Fopen.WriteLine (")") Fopen.WriteLine ("tablespace ODSDATA\';") Fopen.WriteLine ("END;") Fopen.WriteLine ("/") End If Fopen.Close Set Fopen = Nothing Else \'建表头 Set Fcreate = FSO.CreateTextFile(ThisWorkbook.Path & "\\DB\\DBS\\" & mysheet1.Range("A2").Value & "\\Tables\\" & mysheet.Range("A" & i).Value & ".tab", True) Fcreate.WriteLine ("DECLARE ") Fcreate.WriteLine ("v_tab_count NUMBER;") Fcreate.WriteLine ("BEGIN") Fcreate.WriteLine (" SELECT COUNT(*) INTO v_tab_count") Fcreate.WriteLine (" FROM ALL_TABLES t ") Fcreate.WriteLine (" WHERE t.OWNER=\'" & mysheet1.Range("A2").Value & "\'") Fcreate.WriteLine (" AND t.TABLE_NAME=\'" & mysheet.Range("A" & i).Value & "\'") Fcreate.WriteLine (" ;") Fcreate.WriteLine (" IF v_tab_count>0 THEN") Fcreate.WriteLine (" EXECUTE IMMEDIATE \'drop table " & mysheet1.Range("A2").Value & "." & mysheet.Range("A" & i).Value & "\';") Fcreate.WriteLine (" END IF;") Fcreate.WriteLine ("") Fcreate.WriteLine (" EXECUTE IMMEDIATE \'CREATE TABLE " & mysheet1.Range("A2").Value & "." & mysheet.Range("A" & i).Value & "") Fcreate.Write ("( " & mysheet.Range("C" & i).Value & " " & mysheet.Range("E" & i).Value) If mysheet.Range("G" & i).Value = "Y" Then \'非空处理 Fcreate.WriteLine (" not null") End If Fcreate.Close Set Fcreate = Nothing \'建总调角本 Set Fcreate_run = FSO.OpenTextFile(ThisWorkbook.Path & "\\DB\\PATCH\\run.sql", 8, False) Fcreate_run.WriteLine ("@../DBS/" & mysheet1.Range("A2").Value & "/Tables/" & mysheet.Range("A" & i).Value & ".tab") Fcreate_run.Close Set Fcreate_run = Nothing End If End If Next i \'备注 For i = 2 To mysheet.UsedRange.Rows.Count \'遍历所有的列 If mysheet.Range("H" & i).Value = mysheet1.Range("B2").Value Then \'判断版本是否一致 If FSO.FileExists(ThisWorkbook.Path & "\\DB\\DBS\\" & mysheet1.Range("A2").Value & "\\Tables\\" & mysheet.Range("A" & i).Value & ".tab") Then \'判断SQL文件是否存在 Set Fopen = FSO.OpenTextFile(ThisWorkbook.Path & "\\DB\\DBS\\" & mysheet1.Range("A2").Value & "\\Tables\\" & mysheet.Range("A" & i).Value & ".tab", 8, False) If mysheet.Range("A" & i).Value <> mysheet.Range("A" & i - 1).Value Then Fopen.WriteLine ("-- Add comments to the table ") Fopen.WriteLine ("comment on table " & mysheet1.Range("A2").Value & "." & mysheet.Range("A" & i).Value & "") Fopen.WriteLine (" is \'" & mysheet.Range("B" & i).Value & "\';") Fopen.WriteLine ("-- Add comments to the columns ") Fopen.WriteLine ("comment on column " & mysheet1.Range("A2").Value & "." & mysheet.Range("A" & i).Value & "." & mysheet.Range("C" & i).Value & "") Fopen.WriteLine (" is \'" & mysheet.Range("D" & i).Value & "\';") Else Fopen.WriteLine ("comment on column " & mysheet1.Range("A2").Value & "." & mysheet.Range("A" & i).Value & "." & mysheet.Range("C" & i).Value & "") Fopen.WriteLine (" is \'" & mysheet.Range("D" & i).Value & "\';") End If Fopen.Close Set Fopen = Nothing End If End If Next i Set FSO = Nothing End Sub
以上是关于求教sqlserver通过命令行生成建表语句的主要内容,如果未能解决你的问题,请参考以下文章