本文代码转载自红雨先生
*-----------------------------------------------
* SqlServer 相关函数
*-----------------------------------------------
Clear
*-----------------------------------------------
* 测试 GetTableCode()
*-----------------------------------------------
Local lcDataBaseName, lcTableName, lcTableCode, lnSqlHandle, lcSqlStringConnect
lcTableCode = []
lcDataBaseName = [DCLZ]
lcTableName = "[dbo].[考勤原始表]"
lcSqlStringConnect = [driver={Sql server};server=(local);uid=sa;pwd=Admin;database=Master]
lnSqlHandle = Sqlstringconnect( lcSqlStringConnect )
If lnSqlHandle > 0
= SQLSetprop(lnSqlHandle,[DispWarnings], (.T.))
lcTableCode = GetTableCode( lnSqlHandle, lcDataBaseName, lcTableName )
= SQLDisconnect(lnSqlHandle)
If !Empty(lcTableCode)
_Cliptext = lcTableCode
Modify File ([(]+lcDataBaseName+[)]+lcTableName+[.sql]) Nowait
Keyboard [{CTRL+A}]
Keyboard [{CTRL+V}]
Endif
Endif
Return
*-----------------------------------------------
* 测试 GetProcedreCode()
*-----------------------------------------------
Local lcDataBaseName, lcProcedreName, lcProcedreCode, lnSqlHandle, lcSqlStringConnect
lcProcedreCode = []
lcDataBaseName = [DCLZ]
lcProcedreName = [dbo.ins考勤原始表]
lcSqlStringConnect = [driver={Sql server};server=(local);uid=sa;pwd=Admin;database=Master]
lnSqlHandle = Sqlstringconnect( lcSqlStringConnect )
If lnSqlHandle > 0
= SQLSetprop(lnSqlHandle,[DispWarnings], (.T.))
lcProcedreCode = GetProcedreCode( lnSqlHandle, lcDataBaseName, lcProcedreName )
= SQLDisconnect(lnSqlHandle)
If !Empty(lcProcedreCode)
_Cliptext = lcProcedreCode
Modify File ([(]+lcDataBaseName+[)]+lcProcedreName+[.sql]) Nowait
Keyboard [{CTRL+A}]
Keyboard [{CTRL+V}]
Endif
Endif
Return
*-----------------------------------------------
*-----------------------------------------------
* 函数: 获取SQL Server加密存储过程、函数、
* 视图、触发器 脚本,字节不受限制
* 设计: 红雨
* 时间: 2004.04.01
*-----------------------------------------------
Function GetProcedreCode( tnSqlHandle, tcDataBase, tcObjectName )
*-----------------------------------------------
#Define CR Chr(13)+Chr(10)
Local lcReturn, lcTmpCursor, lcMyProcedure
m.lcReturn = []
m.lcTmpCursor = [T]+Sys(2015)
m.lcDataBase = Iif(Type([m.tcDataBase])=[C] And !Empty(m.tcDataBase), m.tcDataBase, [Master])
If Used(m.lcTmpCursor)
Use In (m.lcTmpCursor)
Endif
*-----------------------------------------------
If Type([m.tnSqlHandle])=[N] And m.tnSqlHandle>0 And Type([m.tcObjectName])=[C]
= Sqlexec(m.tnSqlHandle, "use " + m.lcDataBase )
m.lcMyProcedure = "CREATE PROCEDURE " + m.lcTmpCursor + "( @objectName varchar(50) )" ;
+CR+ " AS" ;
+CR+ " begin" ;
+CR+ " set nocount on" ;
+CR+ " begin tran" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " Declare @objectname1 varchar(100)," ;
+CR+ " @orgvarbin varbinary(8000)" ;
+CR+ " Declare @sql1 nvarchar(4000)," ;
+CR+ " @sql2 varchar(8000)," ;
+CR+ " @sql3 nvarchar(4000)," ;
+CR+ " @sql4 nvarchar(4000)" ;
+CR+ " Declare @OrigSpText1 nvarchar(4000)," ;
+CR+ " @OrigSpText2 nvarchar(4000)," ;
+CR+ " @OrigSpText3 nvarchar(4000)," ;
+CR+ " @resultsp nvarchar(4000)," ;
+CR+ " @ptrval binary(16)," ;
+CR+ " @prvlen Int" ;
+CR+ " Declare @i Int," ;
+CR+ " @Status Int," ;
+CR+ " @Type varchar(10)," ;
+CR+ " @parentid Int" ;
+CR+ " Declare @colid Int," ;
+CR+ " @N Int," ;
+CR+ " @q Int," ;
+CR+ " @j Int," ;
+CR+ " @k Int," ;
+CR+ " @Encrypted Int," ;
+CR+ " @Number Int"
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " Select @Type=xtype, @parentid=parent_obj From sysobjects Where Id=object_id(@ObjectName)" ;
+CR+ " " ;
+CR+ " If exists (select * from dbo.sysobjects where id = object_id(N'#result') and OBJECTPROPERTY(id, N'IsUserTable') = 1)" ;
+CR+ " Drop Table #result" ;
+CR+ " Create Table #result(ProcName varchar(50), ProcCode text)" ;
+CR+ " Insert #result values (@ObjectName, '')" ;
+CR+ " " ;
+CR+ " If exists (select * from dbo.sysobjects where id = object_id(N'#temp') and OBJECTPROPERTY(id, N'IsUserTable') = 1)" ;
+CR+ " Drop Table #temp" ;
+CR+ " Create Table #temp(Number Int,colid Int,ctext varbinary(8000),Encrypted Int,Status Int)" ;
+CR+ " Insert #temp Select Number,colid,ctext,Encrypted,Status From syscomments Where Id = object_id(@objectName)" ;
+CR+ " " ;
+CR+ " Select @Number=Max(Number) From #temp" ;
+CR+ " Set @k=0" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " While @k<=@Number" ;
+CR+ " Begin" ;
+CR+ " If Exists(Select 1 From syscomments Where Id=object_id(@objectname) And Number=@k)" ;
+CR+ " Begin" ;
+CR+ " If @Type='P'" ;
+CR+ " Set @sql1=(" ;
+CR+ " CASE" ;
+CR+ " When @Number>1 Then 'ALTER PROCEDURE '+ @objectName +';'+Rtrim(@k)+' WITH ENCRYPTION AS '" ;
+CR+ " Else 'ALTER PROCEDURE '+ @objectName+' WITH ENCRYPTION AS '" ;
+CR+ " END )" ;
+CR+ " If @Type='TR'" ;
+CR+ " Begin" ;
+CR+ " Declare @parent_obj varchar(255)," ;
+CR+ " @tr_parent_xtype varchar(10)" ;
+CR+ " Select @parent_obj = parent_obj From sysobjects Where Id=object_id(@objectName)" ;
+CR+ " Select @tr_parent_xtype = xtype From sysobjects Where Id=@parent_obj" ;
+CR+ " If @tr_parent_xtype='V'" ;
+CR+ " Begin" ;
+CR+ " Set @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION INSTERD OF INSERT AS PRINT 1 '" ;
+CR+ " End" ;
+CR+ " Else" ;
+CR+ " Begin" ;
+CR+ " Set @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION FOR INSERT AS PRINT 1 '" ;
+CR+ " End" ;
+CR+ " End" ;
+CR+ " If @Type='FN' Or @Type='TF' Or @Type='IF'" ;
+CR+ " Set @sql1=(" ;
+CR+ " Case @Type" ;
+CR+ " When 'TF' Then 'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '" ;
+CR+ " When 'FN' Then 'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns char(1) with encryption as begin return @a end'" ;
+CR+ " When 'IF' Then 'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns table with encryption as return select @a as a'" ;
+CR+ " END )" ;
+CR+ " If @Type='V'" ;
+CR+ " Set @sql1='ALTER VIEW '+@objectname+' WITH ENCRYPTION AS SELECT 1 as f'" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " Set @q=Len(@sql1)" ;
+CR+ " Set @sql1=@sql1+Replicate('-',4000-@q)" ;
+CR+ " Select @sql2=Replicate('-',8000)" ;
+CR+ " Set @sql3='exec(@sql1'" ;
+CR+ " Select @colid=Max(colid) From #temp Where Number=@k" ;
+CR+ " Set @N=1" ;
+CR+ " While @N<=Ceiling(1.0*(@colid-1)/2) And Len(@sQL3)<=3996" ;
+CR+ " Begin" ;
+CR+ " Set @sql3=@sql3+'+@'" ;
+CR+ " Set @N=@N+1" ;
+CR+ " End" ;
+CR+ " Set @sql3=@sql3+')'" ;
+CR+ " Exec sp_executesql @sql3,N'@Sql1 nvarchar(4000),@ varchar(8000)',@sql1=@sql1,@=@sql2" ;
+CR+ " End" ;
+CR+ " Set @k=@k+1" ;
+CR+ " End" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " Set @k=0" ;
+CR+ " While @k<=@Number" ;
+CR+ " Begin" ;
+CR+ " If Exists(Select 1 From syscomments Where Id=object_id(@objectname) And Number=@k)" ;
+CR+ " Begin" ;
+CR+ " Select @colid=Max(colid) From #temp Where Number=@k" ;
+CR+ " Set @N=1" ;
+CR+ " While @N<=@colid" ;
+CR+ " Begin" ;
+CR+ " Select @OrigSpText1=ctext,@Encrypted=Encrypted,@Status=Status From #temp Where colid=@N And Number=@k" ;
+CR+ " Set @OrigSpText3=(Select ctext From syscomments Where Id=object_id(@objectName) And colid=@N And Number=@k)" ;
+CR+ " If @N=1" ;
+CR+ " Begin" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " If @Type='P'" ;
+CR+ " Set @OrigSpText2=(" ;
+CR+ " CASE" ;
+CR+ " When @Number>1 Then 'CREATE PROCEDURE '+ @objectName +';'+Rtrim(@k)+' WITH ENCRYPTION AS '" ;
+CR+ " Else 'CREATE PROCEDURE '+ @objectName +' WITH ENCRYPTION AS '" ;
+CR+ " END )" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " If @Type='FN' Or @Type='TF' Or @Type='IF'" ;
+CR+ " Set @OrigSpText2=(" ;
+CR+ " Case @Type" ;
+CR+ " When 'TF' Then 'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '" ;
+CR+ " When 'FN' Then 'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns char(1) with encryption as begin return @a end'" ;
+CR+ " When 'IF' Then 'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns table with encryption as return select @a as a'" ;
+CR+ " END )" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " If @Type='TR'" ;
+CR+ " Begin" ;
+CR+ " If @tr_parent_xtype='V'" ;
+CR+ " Begin" ;
+CR+ " Set @OrigSpText2='CREATE TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION INSTEAD OF INSERT AS PRINT 1 '" ;
+CR+ " End" ;
+CR+ " Else" ;
+CR+ " Begin" ;
+CR+ " Set @OrigSpText2='CREATE TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION FOR INSERT AS PRINT 1 '" ;
+CR+ " End" ;
+CR+ " End" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " If @Type='V'" ;
+CR+ " Set @OrigSpText2='CREATE VIEW '+@objectname+' WITH ENCRYPTION AS SELECT 1 as f'" ;
+CR+ " " ;
+CR+ " Set @q=4000-Len(@OrigSpText2)" ;
+CR+ " Set @OrigSpText2=@OrigSpText2+Replicate('-',@q)" ;
+CR+ " End" ;
+CR+ " Else" ;
+CR+ " Begin" ;
+CR+ " Set @OrigSpText2=Replicate('-', 4000)" ;
+CR+ " End" ;
+CR+ " "
lcMyProcedure = lcMyProcedure ;
+CR+ " Set @i=1" ;
+CR+ " Set @resultsp = Replicate(N'A', (datalength(@OrigSpText1) / 2))" ;
+CR+ " " ;
+CR+ " While @i<=datalength(@OrigSpText1)/2" ;
+CR+ " Begin" ;
+CR+ " Set @resultsp = Stuff(@resultsp, @i, 1," ;
+CR+ " NCHAR(UNICODE(Substring(@OrigSpText1, @i, 1)) ^" ;
+CR+ " (UNICODE(Substring(@OrigSpText2, @i, 1)) ^" ;
+CR+ " UNICODE(Substring(@OrigSpText3, @i, 1)))))" ;
+CR+ " Set @i=@i+1" ;
+CR+ " End" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " Set @orgvarbin=cast(@OrigSpText1 As varbinary(8000))" ;
+CR+ " Set @resultsp=(" ;
+CR+ " Case " ;
+CR+ " When @Encrypted=1 Then @resultsp" ;
+CR+ " Else convert(nvarchar(4000)," ;
+CR+ " Case " ;
+CR+ " When @Status&2=2 Then uncompress(@orgvarbin)" ;
+CR+ " Else @orgvarbin" ;
+CR+ " END )" ;
+CR+ " END )" ;
+CR+ " Print @resultsp" ;
+CR+ " select @ptrval = TEXTPTR(ProcCode) from #result" ;
+CR+ " select @prvlen = DATALENGTH(ProcCode) from #result" ;
+CR+ " UPDATETEXT #result.ProcCode @ptrval @prvlen 0 @resultsp" ;
+CR+ " "
m.lcMyProcedure = m.lcMyProcedure ;
+CR+ " Set @N=@N+1" ;
+CR+ " End" ;
+CR+ " End" ;
+CR+ " Set @k=@k+1" ;
+CR+ " End" ;
+CR+ " Select * from #result" ;
+CR+ " Drop Table #temp" ;
+CR+ " Drop Table #result" ;
+CR+ " Rollback Tran" ;
+CR+ " End"
*-----------------------------------------------
= Sqlexec(m.tnSqlHandle, "if exists (select * from dbo.sysobjects" ;
+CR+ " where id = object_id(N'[dbo].[" + m.lcTmpCursor + "]')" ;
+CR+ " and OBJECTPROPERTY(id, N'IsProcedure') = 1)" ;
+CR+ " drop procedure [dbo].[" + m.lcTmpCursor + "]")
= Sqlexec(m.tnSqlHandle, m.lcMyProcedure )
= Sqlexec(m.tnSqlHandle, "Exec " + m.lcTmpCursor + " ?tcObjectName", m.lcTmpCursor)
= Sqlexec(m.tnSqlHandle, "if exists (select * from dbo.sysobjects" ;
+CR+ " where id = object_id(N'[dbo].[" + m.lcTmpCursor + "]')" ;
+CR+ " and OBJECTPROPERTY(id, N'IsProcedure') = 1)" ;
+CR+ " drop procedure [dbo].[" + m.lcTmpCursor + "]")
*-----------------------------------------------
If Used(m.lcTmpCursor)
Select (m.lcTmpCursor)
m.lcReturn = Alltrim(ProcCode)
Use In (m.lcTmpCursor)
Endif
Endif
Return m.lcReturn
Endfunc
*-----------------------------------------------
* 函数: 获取SQL Server表脚本(创建、触发器、索引、约束)
* 设计: 红雨
* 时间: 2005.04.01
*-----------------------------------------------
Function GetTableCode( tnSqlHandle, tcDataBase, tcObjectName )
*-----------------------------------------------
#Define CR Chr(13)+Chr(10)
Local lcReturn, lcTmpCursor, lcMyProcedure
m.lcReturn = []
m.lcTmpCursor = [T]+Sys(2015)
m.lcDataBase = Iif(Type([m.tcDataBase])=[C] And !Empty(m.tcDataBase), m.tcDataBase, [Master])
If Used(m.lcTmpCursor)
Use In (m.lcTmpCursor)
Endif
*-----------------------------------------------
If Type([m.tnSqlHandle])=[N] And m.tnSqlHandle>0 And Type([tcObjectName])=[C]
= Sqlexec(m.tnSqlHandle, [use ] + m.lcDataBase )
*-----------------------------------------------
* 建立表的脚本
*-----------------------------------------------
= SQLExec(m.tnSqlhandle,[EXEC sp_MShelpcolumns N'] + m.tcObjectName + [', 512, @orderby = 'id'] , m.lcTmpCursor)
If Used(m.lcTmpCursor)
Select (m.lcTmpCursor)
m.lcReturn = m.lcReturn + CR + [--- 建立表的脚本] + CR
m.lcReturn = m.lcReturn + [If Exists (Select * From dbo.SysObjects Where id = object_id(N']+m.tcObjectName+[')] ;
+ [ and OBJECTPROPERTY(id, N'IsTable') = 1)]
m.lcReturn = m.lcReturn + CR + [ Drop table ] + m.tcObjectName
m.lcReturn = m.lcReturn + CR + [GO]
m.lcReturn = m.lcReturn + CR + [Create Table ] + m.tcObjectName + [(]
Local lTextImage
m.lTextImage = .F.
Scan
m.lTextImage = m.lTextImage Or Inlist(Upper(Alltrim(Col_TypeName)),[TEXT],[NTEXT],[IMAGE])
m.lcReturn = m.lcReturn + CR + " [" + Alltrim(Col_Name) + "] ";
+ "[" + Alltrim(Col_TypeName) + "]" ;
+ Iif(Col_Flags=0, [], " (" + Iif(Isnull(Col_Prec),Alltrim(Str(Col_Len)),Alltrim(Str(Col_Prec))) ;
+ Iif(Isnull(Col_Scale), [], [, ] + Alltrim(Str(Col_Scale))) + ")" ) ;
+ Iif(Isnull(Collation),[],[ Collate ] + Alltrim(Collation)) ;
+ Iif(Col_Identity,[ Identity (1, 1)],[]) ;
+ Iif(Col_Null, [ Null] ,[ Not Null]) + [,]
Endscan
m.lcReturn = Left(m.lcReturn,Len(m.lcReturn)-1) + CR + ") ON [PRIMARY]"
m.lcReturn = m.lcReturn + Iif(m.lTextImage," TEXTIMAGE_ON [PRIMARY]",[]) + CR + [GO] + CR
Use In (m.lcTmpCursor)
Endif
*-----------------------------------------------
* 表的索引脚本
*-----------------------------------------------
= SQLExec(m.tnSqlhandle,[EXEC sp_MShelpindex N'] + m.tcObjectName +[' , NULL, 1] , m.lcTmpCursor)
If Used(m.lcTmpCursor) And Reccount(m.lcTmpCursor) > 0
Local cFieldName, nIndex
Select (m.lcTmpCursor)
m.lcReturn = m.lcReturn + CR + [--- 表的索引脚本] ;
+CR+ [Create] + Iif(.F.,[ Unique],[]) + Iif(indid=1,[ Clustered],[]) ;
+CR+ " Index [" +Alltrim(Name)+ "] On " + m.tcObjectName +[ (]
For m.nIndex = 1 To 16
m.cFieldName = [IndCol]+Alltrim(Str(nIndex))
If Isnull(Evaluate(m.cFieldName))
Exit
Endif
m.lcReturn = m.lcReturn + "[" + Alltrim(Evaluate(m.cFieldName)) + "]" ;
+ Iif(Bitand(2^(nIndex-1),Descending) = 2^(nIndex-1), [ Desc,], [ ,])
Endfor
m.lcReturn = Left(m.lcReturn,Len(m.lcReturn)-1) + ") "
m.lcReturn = m.lcReturn ;
+CR+ [With] ;
+CR+ [ DROP_EXISTING] ;
+CR+ " ON [PRIMARY]" + CR + [GO] + CR
Use In (m.lcTmpCursor)
Endif
*-----------------------------------------------
* 约束和触发器
*-----------------------------------------------
= Sqlexec(m.tnSqlHandle, [select Name,xType from SysObjects ] ;
+ [ where parent_obj=object_id(N']+m.tcObjectName+[') order by xType], m.lcTmpCursor)
If Used(m.lcTmpCursor)
Select (m.lcTmpCursor)
Scan
m.lcReturn = m.lcReturn + CR
lcName = Alltrim(Name)
Do Case
Case Upper(Alltrim(xType))=[TR]
m.lcReturn = m.lcReturn + [--- 触发器脚本: ] + lcName + CR ;
+ GetProcedreCode( m.tnSqlHandle, m.lcDataBase, lcName )
Case Upper(Alltrim(xType))=[TF]
m.lcReturn = m.lcReturn + [--- 表函数: ] + lcName + CR ;
+ GetProcedreCode( m.tnSqlHandle, m.lcDataBase, lcName )
Case Upper(Alltrim(xType))=[IF]
m.lcReturn = m.lcReturn + [--- 内嵌表函数: ] + lcName + CR ;
+ GetProcedreCode( m.tnSqlHandle, m.lcDataBase, lcName )
Case Upper(Alltrim(xType))=[D]
m.lcReturn = m.lcReturn + [--- 默认值或 DEFAULT 约束: ] + lcName + CR ;
+ [ALTER TABLE ] + m.tcObjectName + [ WITH NOCHECK ADD] + CR ;
+ " CONSTRAINT [" + lcName + "] DEFAULT " ;
+ GetProcedreCode( m.tnSqlHandle, m.lcDataBase, lcName ) ;
+ " FOR [" + Getwordnum(lcName,3,[_]) + "]"
Case Upper(Alltrim(xType))=[C]
m.lcReturn = m.lcReturn + [--- CHECK 约束: ] + lcName + CR ;
+ [ALTER TABLE ] + m.tcObjectName + [ WITH NOCHECK ADD] + CR ;
+ " CONSTRAINT [" + lcName + "] CHECK " ;
+ GetProcedreCode( m.tnSqlHandle, m.lcDataBase, lcName )
Case Upper(Alltrim(xType))=[F]
m.lcReturn = m.lcReturn + [--- FOREIGN KEY 约束: ] + lcName + CR ;
+ [ALTER TABLE ] + m.tcObjectName + [ WITH NOCHECK ADD] + CR ;
+ " CONSTRAINT [" + lcName + "] FOREIGN KEY " ;
+ GetProcedreCode( m.tnSqlHandle, m.lcDataBase, lcName )
Case Upper(Alltrim(xType))=[PK]
m.lcReturn = m.lcReturn + [--- PRIMARY KEY 约束: ] + lcName + CR ;
+ [ALTER TABLE ] + m.tcObjectName + [ ADD] + CR ;
+ " CONSTRAINT [" + lcName + "] PRIMARY KEY " ;
+ GetProcedreCode( m.tnSqlHandle, m.lcDataBase, lcName )
Case Upper(Alltrim(xType))=[UQ]
m.lcReturn = m.lcReturn + [--- UNIQUE 约束: ] + lcName + CR ;
+ [ALTER TABLE ] + m.tcObjectName + [ ADD] + CR ;
+ " CONSTRAINT [" + lcName + "] UNIQUE " ;
+ GetProcedreCode( m.tnSqlHandle, m.lcDataBase, lcName )
Otherwise
Endcase
m.lcReturn = m.lcReturn + CR + [GO] + CR
Endscan
Use In (m.lcTmpCursor)
Endif
*-----------------------------------------------
Endif
Return m.lcReturn
Endfunc