|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQLAB公司。目前MySQL被广泛地应用在Internet上的中小型网站中。server|sqlserver|创立|语句
MSSQLSERVER只能失掉存储历程的创立语句,办法以下:
sp_helptextprocedureName
可是常常我们必要失掉表的创立语句,好比说在数据库晋级的时分判别某个表是不是已改动,大概已有一个表存在,但不晓得它的创立语句是甚么,字段有无束缚,有无主键,创立了哪些索引等等.上面我给出一个存储历程,供读者参考.
该存储历程能够失掉你想失掉的一切的表的创立语句,包含和表有关的索引的创立语句.
SQLSERVER2000下的代码
createprocedureSP_GET_TABLE_INFO
@ObjNamevarchar(128)/*Thetabletogeneratesqlscript*/
as
declare@Scriptvarchar(255)
declare@ColNamevarchar(30)
declare@ColIDTinyInt
declare@UserTypesmallint
declare@TypeNamesysname
declare@LengthTinyInt
declare@PrecTinyInt
declare@ScaleTinyInt
declare@StatusTinyInt
declare@cDefaultint
declare@DefaultIDTinyInt
declare@Const_Keyvarchar(255)
declare@IndIDSmallInt
declare@IndStatusInt
declare@Index_Keyvarchar(255)
declare@DBNamevarchar(30)
declare@strPri_Keyvarchar(255)
/*
**Checktoseethethetableexistsandinitialize@objid.
*/
ifnotExists(Selectnamefromsysobjectswherename=@ObjName)
begin
select@DBName=db_name()
raiserror(15009,-1,-1,@ObjName,@DBName)
return(1)
end
createtable#spscript
(
idintIDENTITYnotnull,
ScriptVarchar(255)NOTNULL,
LastLinetinyint
)
declareCursor_ColumnINSENSITIVECURSOR
forSelecta.name,a.ColID,a.usertype,b.name,a.length,a.prec,a.scale,a.Status,a.cDefault,
casea.cdefaultwhen0thenelse(selectc.Textfromsyscommentscwherea.cdefault=c.id)endconst_key
fromsyscolumnsa,systypesbwhereobject_name(a.id)=@ObjName
anda.usertype=b.usertypeorderbya.ColID
setnocounton
Select@Script=Createtable+@ObjName+(
Insertinto#spscriptvalues(@Script,0)
/*Getcolumninformation*/
openCursor_Column
fetchnextfromCursor_Columninto@ColName,@ColID,@UserType,@TypeName,@Length,@Prec,@Scale,
@Status,@cDefault,@Const_Key
Select@Script=
while(@@FETCH_STATUS-1)
begin
if(@@FETCH_STATUS-2)
begin
Select@Script=@ColName++@TypeName
if@UserTypein(1,2,3,4)
Select@Script=@Script+(+Convert(char(3),@Length)+)
elseif@UserTypein(24)
Select@Script=@Script+(+Convert(char(3),@Prec)+,
+Convert(char(3),@Scale)+)
else
Select@Script=@Script+
if(@Status&0x80)>0
Select@Script=@Script+IDENTITY(1,1)
if(@Status&0x08)>0
Select@Script=@Script+NULL
else
Select@Script=@Script+NOTNULL
if@cDefault>0
Select@Script=@Script+DEFAULT+@Const_Key
end
fetchnextfromCursor_Columninto@ColName,@ColID,@UserType,@TypeName,@Length,@Prec,@Scale,
@Status,@cDefault,@Const_Key
if@@FETCH_STATUS=0
begin
Select@Script=@Script+,
Insertinto#spscriptvalues(@Script,0)
end
else
begin
Insertinto#spscriptvalues(@Script,1)
Insertinto#spscriptvalues(),0)
end
end
CloseCursor_Column
DeallocateCursor_Column
/*Getindexinformation*/
DeclareCursor_IndexINSENSITIVECURSOR
forSelectname,IndID,statusfromsysindexeswhereobject_name(id)=@ObjName
andIndID>0andIndID255orderbyIndID/*增添了对InDid为255的判别*/
OpenCursor_Index
FetchNextfromCursor_Indexinto@ColName,@IndID,@IndStatus
while(@@FETCH_STATUS-1)
begin
if@@FETCH_STATUS-2
begin
declare@iTinyInt
declare@thiskeyvarchar(50)
declare@IndDescvarchar(68)/*stringtobuildupindexdescin*/
Select@i=1
while(@i<=16)
begin
select@thiskey=index_col(@ObjName,@IndID,@i)
if@thiskeyisnull
break
if@i=1
select@Index_Key=index_col(@ObjName,@IndID,@i)
else
select@Index_Key=@Index_Key+,+index_col(@ObjName,@IndID,@i)
select@i=@i+1
end
if(@IndStatus&0x02)>0
Select@Script=Createunique
else
Select@Script=Create
if@IndID=1
select@Script=@Script+clustered
if(@IndStatus&0x800)>0
select@strPri_Key=PRIMARYKEY(+@Index_Key+)
else
select@strPri_Key=
if@IndID>1
select@Script=@Script+nonclustered
Select@Script=@Script+index+@ColName+ON+@ObjName
+(+@Index_Key+)
Select@IndDesc=
/*
**Seeiftheindexisignore_dupkey(0x01).
*/
if@IndStatus&0x01=0x01
Select@IndDesc=@IndDesc+IGNORE_DUP_KEY+,
/*
**Seeiftheindexisignore_dup_row(0x04).
*/
/*if@IndStatus&0x04=0x04*/
/*Select@IndDesc=@IndDesc+IGNORE_DUP_ROW+,*//*2000不在撑持*/
/*
**Seeiftheindexisallow_dup_row(0x40).
*/
if@IndStatus&0x40=0x40
Select@IndDesc=@IndDesc+ALLOW_DUP_ROW+,
if@IndDesc
begin
Select@IndDesc=SubString(@IndDesc,1,DataLength(@IndDesc)-1)
Select@Script=@Script+WITH+@IndDesc
end
/*
**Addthelocationofthedata.
*/
end
if(@strPri_Key=)
Insertinto#spscriptvalues(@Script,0)
else
update#spscriptsetScript=Script+@strPri_KeywhereLastLine=1
FetchNextfromCursor_Indexinto@ColName,@IndID,@IndStatus
end
CloseCursor_Index
DeallocateCursor_Index
SelectScriptfrom#spscript
setnocountoff
return(0)
SQLSERVER6.5下的代码
createprocedureSP_GET_TABLE_INFO
@ObjNamevarchar(128)/*Thetabletogeneratesqlscript*/
as
declare@Scriptvarchar(255)
declare@ColNamevarchar(30)
declare@ColIDTinyInt
declare@UserTypesmallint
declare@TypeNamesysname
declare@LengthTinyInt
declare@PrecTinyInt
declare@ScaleTinyInt
declare@StatusTinyInt
declare@cDefaultint
declare@DefaultIDTinyInt
declare@Const_Keyvarchar(255)
declare@IndIDSmallInt
declare@IndStatusSmallInt
declare@Index_Keyvarchar(255)
declare@SegmentSmallInt
declare@DBNamevarchar(30)
declare@strPri_Keyvarchar(255)
/*
**Checktoseethethetableexistsandinitialize@objid.
*/
ifnotExists(Selectnamefromsysobjectswherename=@ObjName)
begin
select@DBName=db_name()
raiserror(15009,-1,-1,@ObjName,@DBName)
return(1)
end
createtable#spscript
(
idintIDENTITYnotnull,
ScriptVarchar(255)NOTNULL,
LastLinetinyint
)
declareCursor_ColumnINSENSITIVECURSOR
forSelecta.name,a.ColID,a.usertype,b.name,a.length,a.prec,a.scale,a.Status,a.cDefault,
casea.cdefaultwhen0thenelse(selectcasec.textwhen"()"then"()"elsec.textend
fromsyscommentscwherea.cdefault=c.id)endconst_key
fromsyscolumnsa,systypesbwhereobject_name(a.id)=@ObjName
anda.usertype=b.usertypeorderbya.ColID
setnocounton
Select@Script=Createtable+@ObjName+(
Insertinto#spscriptvalues(@Script,0)
/*Getcolumninformation*/
openCursor_Column
fetchnextfromCursor_Columninto@ColName,@ColID,@UserType,@TypeName,@Length,@Prec,@Scale,
@Status,@cDefault,@Const_Key
Select@Script=
while(@@FETCH_STATUS-1)
begin
if(@@FETCH_STATUS-2)
begin
Select@Script=@ColName++@TypeName
if@UserTypein(1,2,3,4)
Select@Script=@Script+(+Convert(char(3),@Length)+)
elseif@UserTypein(24)
Select@Script=@Script+(+Convert(char(3),@Prec)+,
+Convert(char(3),@Scale)+)
else
Select@Script=@Script+
if(@Status&0x80)>0
Select@Script=@Script+IDENTITY(1,1)
if(@Status&0x08)>0
Select@Script=@Script+NULL
else
Select@Script=@Script+NOTNULL
if@cDefault>0
Select@Script=@Script+DEFAULT+@Const_Key
end
fetchnextfromCursor_Columninto@ColName,@ColID,@UserType,@TypeName,@Length,@Prec,@Scale,
@Status,@cDefault,@Const_Key
if@@FETCH_STATUS=0
begin
Select@Script=@Script+,
Insertinto#spscriptvalues(@Script,0)
end
else
begin
Insertinto#spscriptvalues(@Script,1)
Insertinto#spscriptvalues(),0)
end
end
CloseCursor_Column
DeallocateCursor_Column
/*Getindexinformation*/
DeclareCursor_IndexINSENSITIVECURSOR
forSelectname,IndID,status,Segmentfromsysindexeswhereobject_name(id)=@ObjName
andIndID>0andIndID255orderbyIndID
OpenCursor_Index
FetchNextfromCursor_Indexinto@ColName,@IndID,@IndStatus,@Segment
while(@@FETCH_STATUS-1)
begin
if@@FETCH_STATUS-2
begin
declare@iTinyInt
declare@thiskeyvarchar(50)
declare@IndDescvarchar(68)/*stringtobuildupindexdescin*/
Select@i=1
while(@i<=16)
begin
select@thiskey=index_col(@ObjName,@IndID,@i)
if@thiskeyisnull
break
if@i=1
select@Index_Key=index_col(@ObjName,@IndID,@i)
else
select@Index_Key=@Index_Key+,+index_col(@ObjName,@IndID,@i)
select@i=@i+1
end
if(@IndStatus&0x02)>0
Select@Script=Createunique
else
Select@Script=Create
if@IndID=1
select@Script=@Script+clustered
if(@IndStatus&0x800)>0
select@strPri_Key=PRIMARYKEY(+@Index_Key+)
else
select@strPri_Key=
if@IndID>1
select@Script=@Script+nonclustered
Select@Script=@Script+index+@ColName+ON+@ObjName
+(+@Index_Key+)
Select@IndDesc=
/*
**Seeiftheindexisignore_dupkey(0x01).
*/
if@IndStatus&0x01=0x01
Select@IndDesc=@IndDesc+IGNORE_DUP_KEY+,
/*
**Seeiftheindexisignore_dup_row(0x04).
*/
if@IndStatus&0x04=0x04
Select@IndDesc=@IndDesc+IGNORE_DUP_ROW+,
/*
**Seeiftheindexisallow_dup_row(0x40).
*/
if@IndStatus&0x40=0x40
Select@IndDesc=@IndDesc+ALLOW_DUP_ROW+,
if@IndDesc
begin
Select@IndDesc=SubString(@IndDesc,1,DataLength(@IndDesc)-1)
Select@Script=@Script+WITH+@IndDesc
end
/*
**Addthelocationofthedata.
*/
if@Segment1
select@Script=@Script+ON+name
fromsyssegments
wheresegment=@Segment
end
if(@strPri_Key=)
Insertinto#spscriptvalues(@Script,0)
else
update#spscriptsetScript=Script+@strPri_KeywhereLastLine=1
FetchNextfromCursor_Indexinto@ColName,@IndID,@IndStatus,@Segment
end
CloseCursor_Index
DeallocateCursor_Index
SelectScriptfrom#spscriptorderbyid
setnocountoff
return(0)
mysql使用内部操作字符集gbk来进行操作,即执行"SELECT*FROMtestWHEREname=xxxor1=1/*LIMIT1";从而注入成功 |
|