Yesterday, it was depressing that the database was not synchronized. The database for development and testing was not the same. The database for development was updated frequently. Sometimes even the table structure changes.
This causes many problems. At half past one, I still don't know what the problem is. I am so confused that I want to writeProgramTo compare the differences between the two databases for testing and implementation.
This morning, I wrote during a meeting and passed the test at noon.
There are two SQL statements:
The SQL statements for querying different table and view structures and column attributes in the database are as follows:
Select a. [name] As tablename,
B. [name] As colname,
B. xtype,
B. xusertype,
B. length,
B. colid,
B. cdefault,
B. domain,
B. Number,
B. offset,
B. status,
B. type,
B. usertype,
B. prec,
B. scale,
B. iscomputed,
B. isoutparam,
B. isnullable,
C. column_default,
DBO. fniscolumnprimarykey (B. [ID], B. [name]) as pkey
From sysobjects A, syscolumns B, information_schema. Columns C
Where a. ID = B. ID
And a. xtype in ('U', 'V ')
And a. Name = C. table_name
And B. Name = C. column_name
Order by A. [ID], B. [name]
///////
The meaning of each column is as follows:
Name: the name of the sysname column or process parameter.
Id INT: The table Object ID to which the column belongs, or the Stored Procedure ID associated with this parameter.
The physical storage type in xtype tinyint policypes.
Typestat tinyint is only used internally.
The user-defined data type ID of the xusertype smallint extension.
Length the maximum length of physical storage in smallint policypes.
Xprec tinyint is only used internally.
XScale tinyint is only used internally.
Colid smallint column or parameter ID.
Xoffset smallint is only used internally.
Bitpos tinyint is only used internally.
Reserved tinyint is only used internally.
Colstat smallint is for internal use only.
Cdefault INT: the default value ID of the column.
Domain int the rule or check constraint ID of this column.
Number: the sub-process number of the smallint process group (0 indicates a non-process item.
Colorder smallint is only for internal use.
Autoval varbinary (255) is for internal use only.
Offset smallint the offset of the row where the column is located; if it is negative, it indicates a Variable Length Row.
Status tinyint is used to describe the bitmap of a column or parameter attribute:
0x08 = columns allow null values.
0x10 = when the varchar or varbinary column is added, the ANSI fill takes effect. Retain trailing spaces of the varchar column and trailing zeros of the varbinary column.
0x40 = the output parameter.
0x80 = column ID column.
The physical storage type in type tinyint policypes.
User-Defined data type ID in usertype smallint policypes.
Printfmt varchar (255) is for internal use only.
Prec smallint.
Scale int the number of decimal places in the column.
Iscomputed int indicates whether the flag of this column has been calculated:
0 = not calculated.
1 = calculated.
Isoutparam int indicates whether the process parameter is an output parameter:
1 = true.
0 = false.
Isnullable int indicates whether the column allows null values:
1 = true.
0 = false.
Column_default Default Value
Pkey primary key
.
////////
The following functions are used:
---------------------
Create Function DBO. fniscolumnprimarykey (@ stableid int, @ ncolumnname varchar (128 ))
-- Alter function DBO. fniscolumnprimarykey (@ stablename varchar (128), @ ncolumnname varchar (128 ))
Returns bit
As
Begin
Declare @ ntableid int,
@ Nindexid int,
@ I int
Set @ ntableid = @ stableid -- object_id (@ stablename)
Select @ nindexid = indid
From sysindexes
Where id = @ ntableid
And indid between 1 and 254
And (Status & 2048) = 2048
If @ nindexid is null
Return 0
If @ ncolumnname in
(Select SC. [name]
From sysindexkeys Sik
Inner join syscolumns SC on Sik. ID = SC. ID and Sik. colid = SC. colid
Where Sik. ID = @ ntableid
And Sik. indid = @ nindexid)
Begin
Return 1
End
Return 0
End
---------------------
This SQL statement can be used to identify the column attributes of all tables in the database.
By executing the SQL statement twice, you can compare the attributes of the columns in the two databases.
----------------------------------------------------------------
Use the following SQL statement for stored procedures and functions:
Select * From sysobjects where xtype in ('P', 'tf', 'if', 'fn ') order by [name]
You can query all stored procedures and function names. The result of loop query,
Store the function or stored procedure name in a string spobjectname in each loop, and then call the stored procedure: sp_helptext is as follows:
Exec sp_helptext '"+ spobjectname + "'
The returned table is the text class in the spobjectname stored procedure. A row of text in the stored procedure corresponds to a row in the table.
Execute all the SP and functions using exec sp_helptext to get all the stored procedures and functions.Code.
Put the result in a table and compare the similarities and differences.
Reference: SQL Server books online
<Automatic Generation of stored procedures in the database>? Id = 1111)
Now we only compare the column attributes of tables and views, as well as stored procedures and functions.
There is no constraint relationship between tables.
Add.