JohnPhan 发表于 2007-3-23 12:25:46

[转帖]修复identity 类型字段数据的跳跃 续1

<p>下面是SP_IDENTITY.120.SQL,用于&nbsp; 12.0 以后版本 <br/>/*&nbsp; <br/>&nbsp;* SP_IDENTITY.120.SQL - version for ASE 12.0 or later <br/>&nbsp;* (when running ASE 11.9/11.5/11.0, install SP_IDENTITY.119.SQL instead) <br/>&nbsp;*&nbsp; <br/>&nbsp;* Description <br/>&nbsp;* =========== <br/>&nbsp;* This file contains the stored procedure 'sp_identity', which performs some&nbsp; <br/>&nbsp;* useful functions w.r.t. identity columns: <br/>&nbsp;* <br/>&nbsp;*&nbsp;&nbsp; - displays all tables with an identity column in the current database <br/>&nbsp;*&nbsp;&nbsp; - displays the identity value stored on a table's OAM page <br/>&nbsp;*&nbsp;&nbsp; - resets the identity value stored on a table's OAM page <br/>&nbsp;* <br/>&nbsp;* For details and background, see <a href="http://www.sypron.nl/idfix.html">http://www.sypron.nl/idfix.html</a> . <br/>&nbsp;* <br/>&nbsp;* <br/>&nbsp;* Installation <br/>&nbsp;* ============ <br/>&nbsp;* Execute this script using "isql", using a login having both "sa_role" <br/>&nbsp;* and "sso_role". <br/>&nbsp;* The stored procedure will be created in the sybsystemprocs database. <br/>&nbsp;* <br/>&nbsp;* <br/>&nbsp;* Notes <br/>&nbsp;* ===== <br/>&nbsp;* - Traceflag 3604 should be enabled before running sp_identity for a&nbsp; <br/>&nbsp;*&nbsp;&nbsp; specific table ("dbcc traceon(3604)"); if omitted, you won't see&nbsp; <br/>&nbsp;*&nbsp;&nbsp; any output. <br/>&nbsp;* <br/>&nbsp;* - To run sp_identity for a specific table, sa_role and sybase_ts_role&nbsp; <br/>&nbsp;*&nbsp;&nbsp; are required. <br/>&nbsp;* <br/>&nbsp;* - sp_identity was successfully tested on ASE versions 11.0.3, 11.5,&nbsp; <br/>&nbsp;*&nbsp;&nbsp; 11.9.2 and 12.0 on various platforms. <br/>&nbsp;* <br/>&nbsp;* - sp_identity will not work on ASE 11.0 because this version&nbsp; <br/>&nbsp;*&nbsp;&nbsp; does not support the 'case' expression. <br/>&nbsp;* <br/>&nbsp;* - sp_identity was not tested on a 64-bit ASE version. <br/>&nbsp;* <br/>&nbsp;* <br/>&nbsp;* Revision History <br/>&nbsp;* ================ <br/>&nbsp;* Version 1.0&nbsp; 17-Dec-2000&nbsp; First version <br/>&nbsp;* Version 1.1&nbsp; Oct-2002&nbsp; Handle identity_gap=NULL (thanks to Alan Cooper), <br/>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and some small improvements for installation <br/>&nbsp;* Version 1.2&nbsp; Apr-2003&nbsp; Improved installation <br/>&nbsp;*&nbsp; <br/>&nbsp;* <br/>&nbsp;* Copyright Note &amp; Disclaimer : <br/>&nbsp;* ============================= <br/>&nbsp;* This software is provided "as is"; there is no warranty of any kind. <br/>&nbsp;* While this software is believed to work accurately, it may not work&nbsp; <br/>&nbsp;* correctly and/or reliably in a production environment. In no event shall&nbsp;&nbsp; <br/>&nbsp;* Rob Verschoor and/or Sypron B.V. be liable for any damages resulting&nbsp; <br/>&nbsp;* from the use of this software.&nbsp; <br/>&nbsp;* You are allowed to use this software free of charge for your own&nbsp; <br/>&nbsp;* professional, non-commercial purposes.&nbsp; <br/>&nbsp;* You are not allowed to sell or bundle this software or use it for any&nbsp; <br/>&nbsp;* other commercial purpose without prior written permission from&nbsp; <br/>&nbsp;* Rob Verschoor/Sypron B.V. <br/>&nbsp;* You may (re)distribute only unaltered copies of this software, which&nbsp; <br/>&nbsp;* must include this copyright note, as well as the copyright note in&nbsp; <br/>&nbsp;* the header of each stored procedure. <br/>&nbsp;* <br/>&nbsp;* Note: All trademarks are acknowledged. <br/>&nbsp;* <br/>&nbsp;* Please send any comments, bugs, suggestions etc. to the below email <br/>&nbsp;* address. <br/>&nbsp;* <br/>&nbsp;* Copyright (c) 2000-2002 Rob Verschoor/Sypron B.V. <br/>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; P.O.Box 10695 <br/>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2501 HR Den Haag <br/>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The Netherlands <br/>&nbsp;* <br/>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Email: <a href="mailto:rob@sypron.nl">rob@sypron.nl</a>
                <br/>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WWW&nbsp; : <a href="http://www.sypron.nl/">http://www.sypron.nl/</a>
                <br/>&nbsp;*----------------------------------------------------------------------------&nbsp; <br/>&nbsp;*/ </p><p>set nocount on <br/>go <br/>set flushmessage on <br/>go </p><p>use sybsystemprocs <br/>go </p><p>-- we need to be at ASE 12.0 or later; if not, abort this script <br/>if isnull(object_id("master.dbo.sysqueryplans"),99) &gt;= 99 <br/>begin <br/>&nbsp;&nbsp; print "" <br/>&nbsp;&nbsp; print "" <br/>&nbsp;&nbsp; print "********************************************" <br/>&nbsp;&nbsp; print "********************************************" <br/>&nbsp;&nbsp; print " This script is for ASE 12.0 or later." <br/>&nbsp;&nbsp; print " Please install SP_IDENTITY.119.SQL instead." <br/>&nbsp;&nbsp; print "********************************************" <br/>&nbsp;&nbsp; print "********************************************" <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print "" <br/>&nbsp;&nbsp; set background on&nbsp; -- terminate this script now <br/>end <br/>go </p><p>print "" <br/>print "Installing 'sp_identity'..." <br/>print "" <br/>go </p>

JohnPhan 发表于 2007-3-23 12:28:12

回复:(JohnPhan)[转帖]修复identity 类型字段数据的...

<p>if object_id("sp_identity_help") &lt;&gt; NULL <br/>begin <br/>&nbsp;&nbsp; drop proc sp_identity_help <br/>end <br/>go </p><p>create proc sp_identity_help <br/>/* Copyright (c) 2000-2002 Rob Verschoor/Sypron B.V. */ <br/>as <br/>begin <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print " Usage: sp_identity table_name, ""hex-string-from-OAM-page"", new-identity-value " <br/>&nbsp;&nbsp; print " Notes: " <br/>&nbsp;&nbsp; print " - specifying only parameter 1 retrieves the current identity value for" <br/>&nbsp;&nbsp; print "&nbsp;&nbsp; that table from the OAM page as a hexadecimal string." <br/>&nbsp;&nbsp; print " - specifying this hexadecimal string as the second parameter (in quotes)" <br/>&nbsp;&nbsp; print "&nbsp;&nbsp; will decode the hex value to a numeric value." <br/>&nbsp;&nbsp; print " - specifying a numeric value for the third parameter will set that" <br/>&nbsp;&nbsp; print "&nbsp;&nbsp; value as the new identity value for this table." <br/>&nbsp;&nbsp; print " - parameters 2 and 3 cannot be specified together: one of them must be NULL." <br/>&nbsp;&nbsp; print " - ""dbcc traceon(3604)"" must be run before using option 2 or 3" <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print " Copyright (c) 2000-2002 Rob Verschoor/Sypron B.V." <br/>&nbsp;&nbsp; print " See <a href="http://www.sypron.nl/idfix.html">http://www.sypron.nl/idfix.html</a> for background information &amp; updates." <br/>&nbsp;&nbsp; print " " <br/>end <br/>go </p><p>grant execute on sp_identity_help to public <br/>dump tran sybsystemprocs with truncate_only <br/>go </p><p>if object_id("sp_identity") &lt;&gt; NULL <br/>begin <br/>&nbsp;&nbsp; drop proc sp_identity <br/>end <br/>go </p><p>create proc sp_identity <br/>/* Copyright (c) 2000-2002 Rob Verschoor/Sypron B.V. */ <br/>&nbsp;@p0 varchar(50) = NULL, -- table name <br/>&nbsp;@p1 varchar(50) = NULL, -- hex value to decode <br/>&nbsp;@p2 numeric(38) = NULL&nbsp; -- new value to set <br/>as <br/>begin <br/>set nocount on <br/>declare @n numeric(38), @j numeric(38), @n256 numeric(3), @n10 numeric(38) <br/>declare @i int, @max int, @idlen int, @idlenb int, @ib int, @len int <br/>declare @p0_id int, @b int, @b1 binary(1), @lsb int <br/>declare @idgap int, @idburn int, @maxidgap numeric(38), @max1 int, @max2 int <br/>declare @idburnpct numeric(5,2), @v int, @ntab int <br/>declare @c2 char(2), @doampg int, @indid int, @dbname varchar(32) <br/>declare @vc50 varchar(50), @colname varchar(32), @vb16 varbinary(16) </p><p>select @dbname = db_name() </p><p>-- check version <br/>select @v = 0 <br/>if exists (select * from sysobjects&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where name = "sysqueryplans" and type = "S") <br/>begin <br/>&nbsp;&nbsp;&nbsp; select @v = 12 <br/>end </p><p>if @p0 = '?' <br/>begin <br/>&nbsp;&nbsp; exec sp_identity_help <br/>&nbsp;&nbsp; return 0 <br/>end </p><p>-- numeric values <br/>select @n256 = 256 <br/>select @n10 = 10 </p><p>-- id burning set factor <br/>select @idburn = value from master.dbo.syscurconfigs where config=141 <br/>select @idburnpct = convert(numeric(5,2), @idburn * 0.00001) </p><p>-- figure out msb/lsb <br/>select @lsb = 0 <br/>if substring(convert(binary(4), 1), 1 ,1) = 0x01 select @lsb = 1 <br/>if substring(convert(binary(4), 1), 2 ,1) = 0x01 select @lsb = 2 <br/>if substring(convert(binary(4), 1), 3 ,1) = 0x01 select @lsb = 3 <br/>if substring(convert(binary(4), 1), 4 ,1) = 0x01 select @lsb = 4 </p><p>-- get all tables containing identity columns in this database <br/>select id = so.id, owner = su.name, uid = so.uid, so.name, si.doampg, si.indid, idgap =&nbsp; </p><p>isnull(si.identitygap,0), <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; colname = sc.name, sc.prec, maxgap = convert(numeric(38), ceiling((@idburn * 0.0000001) *&nbsp; </p><p>power(@n10,prec))) <br/>into #id <br/>from sysindexes si, syscolumns sc, sysobjects so, sysusers su <br/>where si.indid &lt; 2&nbsp; <br/>&nbsp; and so.type = "U"&nbsp; <br/>&nbsp; and sc.status &amp; 128 = 128 <br/>&nbsp; and so.id = sc.id&nbsp; <br/>&nbsp; and so.id = si.id <br/>&nbsp; and so.uid = su.uid <br/>select @ntab = @@rowcount </p><p>set arithabort numeric_truncation off </p><p>if @p0 = null <br/>begin <br/>&nbsp;&nbsp; -- display all tables with identity columns in the current DB <br/>&nbsp;&nbsp; if @ntab = 0 <br/>&nbsp;&nbsp; begin <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print " There are no tables with an identity column in database '%1!'", @dbname <br/>&nbsp; return 0 <br/>&nbsp;&nbsp; end <br/>&nbsp;&nbsp; print " Tables with an identity column in database '%1!':", @dbname <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; select @max1 = max(char_length(owner + "." + name + "." + colname)) + 12 from #id <br/>&nbsp;&nbsp; select @max2 = max(char_length(convert(varchar(38), maxgap))) + 7 from #id </p><p>&nbsp;&nbsp; if @max1 &lt;= 52 and @max2 &lt;= 24 <br/>&nbsp;&nbsp; begin <br/>&nbsp;&nbsp;&nbsp;&nbsp; select convert(varchar(52), owner + "." + name + "." + colname + " numeric(" +&nbsp; </p><p>convert(varchar(2),prec) + ")") "Owner.Table.Column datatype", <br/>&nbsp;&nbsp;&nbsp; convert(varchar(24), case when idgap = 0 then convert(varchar(38),maxgap) + "&nbsp; </p><p>(burn)" <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else convert(varchar, idgap) + " (identity_gap)" <br/>&nbsp;&nbsp;&nbsp;&nbsp; end) "Maximum Identity Gap" </p><p>&nbsp;&nbsp;&nbsp;&nbsp; from #id <br/>&nbsp;&nbsp;&nbsp;&nbsp; order by name <br/>&nbsp;&nbsp; end <br/>&nbsp;&nbsp; else <br/>&nbsp;&nbsp; if @max1 &lt;= 32 and @max2 &lt;= 44 <br/>&nbsp;&nbsp; begin <br/>&nbsp;&nbsp;&nbsp;&nbsp; select convert(varchar(32), owner + "." + name + "." + colname + " numeric(" +&nbsp; </p><p>convert(varchar(2),prec) + ")") "Owner.Table.Column datatype", <br/>&nbsp;&nbsp;&nbsp; convert(varchar(44), case when idgap = 0 then convert(varchar(38),maxgap) + "&nbsp; </p><p>(burn)" <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else convert(varchar, idgap) + " (identity_gap)" <br/>&nbsp;&nbsp;&nbsp;&nbsp; end) "Maximum Identity Gap" </p><p>&nbsp;&nbsp;&nbsp;&nbsp; from #id <br/>&nbsp;&nbsp;&nbsp;&nbsp; order by name <br/>&nbsp;&nbsp; end <br/>&nbsp;&nbsp; else <br/>&nbsp;&nbsp; begin <br/>&nbsp;&nbsp;&nbsp;&nbsp; select convert(varchar(78), owner + "." + name + "." + colname + " numeric(" +&nbsp; </p><p>convert(varchar(2),prec) + ")") "Owner.Table.Column datatype", <br/>&nbsp;&nbsp;&nbsp; convert(varchar(50), case when idgap = 0 then convert(varchar(38),maxgap) + "&nbsp; </p><p>(burn)" <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else convert(varchar, idgap) + " (identity_gap)" <br/>&nbsp; end) "Maximum Identity Gap" <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from #id <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; order by name <br/>&nbsp;&nbsp; end </p><p>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print " Legend:" <br/>&nbsp;&nbsp; print "&nbsp;&nbsp;&nbsp; (burn)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : gap size is determined by ""identity burning set factor"" " <br/>&nbsp;&nbsp; if @v = 12 <br/>&nbsp;&nbsp; begin <br/>&nbsp;&nbsp; print "&nbsp;&nbsp;&nbsp; (identity_gap) : gap size is determined by the ""identity_gap"" setting" <br/>&nbsp;&nbsp; end <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print " Current value for ""identity burning set factor"" = %1! (=%2!%%)", @idburn, @idburnpct </p><p><br/>&nbsp;&nbsp; return 0 <br/>end </p>

JohnPhan 发表于 2007-3-23 12:28:48

回复:(JohnPhan)[转帖]修复identity 类型字段数据的...

<p>if charindex("sa_role", show_role()) = 0 <br/>begin&nbsp; <br/>&nbsp;&nbsp; print "You must have 'sa_role' to run this procedure." <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>if charindex("sybase_ts_role", show_role()) = 0 <br/>begin&nbsp; <br/>&nbsp;&nbsp; print "You must have 'sybase_ts_role' to run this procedure." <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>-- get some info on the object <br/>select @p0_id = object_id(@p0) <br/>if @p0_id = NULL <br/>begin <br/>&nbsp;&nbsp; print " Error: '%1!' is not a user table.", @p0 <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>if @p0 not like "%.%" <br/>begin <br/>&nbsp; select @ntab = count(*) from sysobjects where name = @p0 and type = "U" </p><p>&nbsp; if @ntab &gt; 1 <br/>&nbsp; begin <br/>&nbsp;&nbsp;&nbsp;&nbsp; print " %1! tables named '%2!' exist in this database:", @ntab, @p0 <br/>&nbsp;print " " <br/>&nbsp;&nbsp;&nbsp;&nbsp; select owner + "." + name "owner.table_name"&nbsp; <br/>&nbsp;from #id where name = @p0 order by uid <br/>&nbsp;print " " <br/>&nbsp;&nbsp;&nbsp;&nbsp; print " Specify 'owner.table_name' to identify the table." <br/>&nbsp;return -1 <br/>&nbsp; end <br/>end </p><p>select @doampg = doampg, @indid = indid, @idgap = idgap <br/>from #id where id = @p0_id </p><p>if not exists (select 1 from syscolumns where id = @p0_id and status &amp; 128 = 128)&nbsp;&nbsp; <br/>begin <br/>&nbsp;&nbsp; print " Error: Table '%1!' does not have an identity column", @p0 <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>-- retrieve the length of the numeric identity column as declared when the <br/>-- table was created <br/>select @len = prec, @colname = name <br/>from syscolumns where id = @p0_id and status &amp; 128 = 128 <br/>if @@rowcount = 0 <br/>begin <br/>&nbsp;&nbsp; print " Error: Could not retrieve length of identity column for table '%1!'", @p0 <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>select @idlen = @len -- column precision (positions) <br/>select @idlenb = (@idlen+1-((@idlen+5)/12)-(@idlen/12))/2 -- column length (bytes) </p><p>print " " <br/>print " Table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = %1! (id=%2!)", @p0, @p0_id <br/>print " Identity column&nbsp;&nbsp; = %1! numeric(%2!)", @colname, @len <br/>--print " OAM page&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = %1!", @doampg <br/>--print " Bytes on OAM page = %1!", @idlenb <br/>if @idgap &gt; 0 <br/>begin <br/>&nbsp;&nbsp; print " Max. Identity Gap = %1! (""identity_gap"" = %2!)", @idgap, @idgap <br/>end <br/>else <br/>begin <br/>&nbsp;&nbsp; set arithabort numeric_truncation off <br/>&nbsp;&nbsp; select @maxidgap = ceiling((convert(numeric(38),@idburn) * 0.0000001) * power(@n10,@len)) <br/>&nbsp;&nbsp; print " Max. Identity Gap = %1!", @maxidgap <br/>&nbsp;&nbsp; print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (""identity burning set factor"" = %1! = %2!%%)", @idburn, @idburnpct <br/>&nbsp;&nbsp; if @v = 12 <br/>&nbsp;&nbsp; begin <br/>&nbsp;&nbsp; print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (Note: ""identity_gap"" has not been set for this table !)"&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp; end <br/>end <br/>print " " </p><p>if @p1 = NULL and @p2 = NULL -- retrieve current ID value <br/>begin <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print " Reading identity value from OAM page... (traceflag 3604 should be enabled !)" <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; dbcc object_atts (@p0, 0, get) <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print " To decode this hexadecimal value, re-run 'sp_identity' with the hexadecimal" <br/>&nbsp;&nbsp; print " string as a quoted 2nd parameter (you can leave the spaces in). " <br/>&nbsp;&nbsp; print " Example:&nbsp; sp_identity %1!, ""the-hex-string-from-the-above-output"" ", @p0 <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; -- <br/>&nbsp;&nbsp; -- only for 12.0+, dbcc listoam will display the values in the OAM and the DES <br/>&nbsp;&nbsp; -- <br/>&nbsp;&nbsp; --dbcc listoam(@dbname, @p0_id, @indid) <br/>&nbsp;&nbsp; -- <br/>&nbsp;&nbsp; return 0 <br/>end </p><p>if upper(@p1) like "[ 0-9A-F]%" -- entered a hex string, decode it <br/>begin </p><p>if @p2 != NULL <br/>begin <br/>&nbsp;&nbsp; print " Error: When decoding a hex value from the OAM page, parameter 3" <br/>&nbsp;&nbsp; print " (the new identity value) must be NULL." <br/>&nbsp;&nbsp; exec sp_identity_help <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>select @ib = @idlenb -- counts bytes <br/>select @i&nbsp; = char_length(@p1) -- counts string <br/>select @n = 0 -- holds result </p><p>while 1 = 1 <br/>begin <br/>&nbsp;&nbsp; select @c2 = substring(@p1, (char_length(@p1)-@i)+1, 2) </p><p>&nbsp;&nbsp; --print "Step %1!, [%2!]", @ib, @c2 <br/>&nbsp;&nbsp; if upper(substring(@c2,1,1)) = " " -- ignore single space pasted in by user <br/>&nbsp;&nbsp; begin <br/>&nbsp;&nbsp;&nbsp;&nbsp; select @i = @i - 1 <br/>&nbsp;continue <br/>&nbsp;&nbsp; end </p><p>&nbsp;&nbsp; if (upper(@c2) not like "")&nbsp; <br/>&nbsp;&nbsp; begin <br/>&nbsp;&nbsp;&nbsp;&nbsp; print " Error: Invalid characters (%1!) in hex string", @c2 <br/>&nbsp;&nbsp;&nbsp;&nbsp; break <br/>&nbsp;&nbsp; end </p><p>&nbsp;&nbsp; -- add this byte to the result <br/>&nbsp;&nbsp; select @vc50 = @vc50 + @c2 <br/>&nbsp;&nbsp; select @j = power(@n256, (@ib-1)) <br/>&nbsp;&nbsp; select @n = @n + @j * hextoint("0x" + @c2) <br/>&nbsp;&nbsp; --print "Step %1!, exp= %2!, result = %3!", @ib, @j, @n </p><p>&nbsp;&nbsp; -- next byte <br/>&nbsp;&nbsp; select @ib = @ib - 1&nbsp; <br/>&nbsp;&nbsp; if @ib = 0 break&nbsp; -- ready </p><p>&nbsp;&nbsp; -- next 2 chars <br/>&nbsp;&nbsp; select @i = @i - 2 <br/>&nbsp;&nbsp; if @i = 0&nbsp; <br/>&nbsp;&nbsp; begin <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- we shouldn't have got here, error <br/>&nbsp; print " Error: ID column length and specified hex string do not match !" <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break <br/>&nbsp;&nbsp; end <br/>end </p><p>if @ib = 0 <br/>begin <br/>&nbsp;&nbsp; print " Decoded identity value on OAM page = %1!", @n <br/>&nbsp;&nbsp; print " (hex= %1!)", @vc50 <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; return 0 <br/>end <br/>else <br/>begin <br/>&nbsp;&nbsp; print " Error: Aborted due to error." <br/>&nbsp;&nbsp; return -1 <br/>end <br/>end </p><p>if @p2 != NULL -- entered a new ID value, set it <br/>begin </p><p>if @p1 != NULL <br/>begin <br/>&nbsp;&nbsp; print " Error: When setting a new identity value, parameter 2" <br/>&nbsp;&nbsp; print " (the hex string from the OAM page) must be NULL." <br/>&nbsp;&nbsp; exec sp_identity_help <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>-- set the new ID value <br/>if @p2 &lt;= 0 <br/>begin <br/>&nbsp;&nbsp; print " Error: The new identity value must be &gt;= 0." <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; exec sp_identity_help <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>if char_length(convert(varchar, @p2)) &gt; @idlen <br/>begin <br/>&nbsp;&nbsp; select @vc50 = replicate("9", @idlen) <br/>&nbsp;&nbsp; print " Error: New identity value (%1!) is too large", @p2 <br/>&nbsp;&nbsp; print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for this identity column. The maximum possible value", @p2 <br/>&nbsp;&nbsp; print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is %1! (%2! positions).", @vc50, @idlen <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>if @lsb = 0 <br/>begin <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print "Internal error in 'sp_identity':" <br/>&nbsp;&nbsp; print "Cannot figure out byte order on this platform." <br/>&nbsp;&nbsp; print "Please send a note to <a href="mailto:'rob@sypron.nl'">'rob@sypron.nl'</a> with the text of this" <br/>&nbsp;&nbsp; print "error and your @@version string." <br/>&nbsp;&nbsp; return -1 <br/>end </p><p>-- convert the numeric value to hex <br/>select @ib = @idlenb -- counts bytes <br/>select @i&nbsp; = 15 </p><p>select @vc50 = NULL -- holds result <br/>select @vb16 = NULL -- holds result <br/>select @n256 = 256 <br/>select @n = @p2 <br/>set arithabort numeric_truncation off </p><p>while 1 = 1 <br/>begin <br/>&nbsp;&nbsp; select @j = power(@n256, @i) </p><p>&nbsp;&nbsp; select @b = @n / @j <br/>&nbsp;&nbsp; select @n = @n - (@b * @j) <br/>&nbsp;&nbsp; select @c2 = right(inttohex(@b),2) <br/>&nbsp;&nbsp; select @b1 = substring(convert(binary(4), @b), @lsb,1) </p><p>&nbsp;&nbsp; select @vb16 = @vb16 + @b1 </p><p>&nbsp;&nbsp; --print "Step %1!, @b=%2!, hex(c2)=%3!, hex(b1)=%5!, result=%4!", @i, @b, @c2, @vb16, @b1 </p><p>&nbsp;&nbsp; select @i = @i - 1&nbsp; <br/>&nbsp;&nbsp; if @i &lt; 0 break&nbsp; -- ready <br/>end </p><p>if @i &lt; 0 <br/>begin <br/>&nbsp;&nbsp; select @vb16 = substring(@vb16, 16 - (@idlenb) + 1, (@idlenb) ) + substring(@vb16, 1, 16 -&nbsp; </p><p>(@idlenb) ) </p><p>&nbsp;&nbsp; -- set the new ID value <br/>&nbsp;&nbsp; dbcc object_atts(@p0, 0, "put", @vb16) </p><p>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; print " Identity value on OAM page has been set to %1!", @p2 <br/>&nbsp;&nbsp; print " (hex= %1!)", @vb16 <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; select @p2 = @p2 + 1 <br/>&nbsp;&nbsp; print " You should now do a 'shutdown with nowait' immediately." <br/>&nbsp;&nbsp; print " After restarting the server, the value assigned to the next row " <br/>&nbsp;&nbsp; print " inserted into '%1!' will be %2!.", @p0, @p2 <br/>&nbsp;&nbsp; print " " <br/>&nbsp;&nbsp; return 0 <br/>end <br/>else <br/>begin <br/>&nbsp;&nbsp; print " Error: Aborted due to error." <br/>&nbsp;&nbsp; return -1 <br/>end <br/>&nbsp;&nbsp;&nbsp; <br/>end </p><p>-- we should never get here <br/>print "Internal error in 'sp_identity': Reached invalid end of procedure." <br/>return -1 <br/>end <br/>go </p><p>grant execute on sp_identity to public <br/>dump tran sybsystemprocs with truncate_only <br/>go </p><p><br/>print "Ready. For usage information, run 'sp_identity ""?"" '." <br/>print "" <br/>go </p><p>/* <br/>** end <br/>*/ <br/></p>
页: [1]
查看完整版本: [转帖]修复identity 类型字段数据的跳跃 续1

免责声明:
本站所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:Admin@SybaseBbs.com