Note: SQL Server 2005 and above support. Version estimates are not supported (working environment 2005,2008).
To work, you need to add a new column to the existing table in SQL Server and add a description. Thus there is a stored procedure as follows. (Attach the stored pro
SQL server often has the following problems:After a table uses columns with automatic numbers, tens of thousands of columns have been automatically numbered due to a large amount of data being tested. Now we are using this table, and the test data has been deleted. The legacy problem is that we are entering new data and the number will only be increased, deleted numbers cannot be used. Who knows how to solv
'the value of the Key5'
fromheadertable awhereA.headerid= 10000
SELECTA.*, t.a0001 as 'the value of the Key1', t.a0002 as 'the value of the Key2', t.a0003 as 'the value of the Key3', t.a0004 as 'the value of the Key4', t.a0005 as 'the value of the Key5'
fromHeadertable AInner Join
(SelectHeaderid, Detailkey, detailvalues fromdetailtable) T Pivot (MAX(detailvalues) forDetailkeyinch(a0001,a0002,a0003,a0004,a0005)) T onT.headerid=A.headeridwhereA.headerid= 10000
I. Definition and characteristics of the identity columnThe identity column in SQL Server is also called the identifier column, which is also known as the self-increment column.This type of column has the following three characteristics :1. The data type of the
that for composite index, statistics only the leading column of statistics, that is, idx_col3_col2 this index only COL2 this column of statistics, as follows For col2=2 statistics, the statistic is 100336 lines, we remember this number Another feature of statistics is the automatic creation of statistics on query columns (non-indexed columns), which is automatically created in the following query executi
1: Add a field to the tableAlter table [table name] add [column name] Type2: Delete FieldAlter table [table name] Drop column [column name]3: Modify the field type in the table (you can modify the type of the column, whether it is empty)ALTER TABLE [table name]
constraint F = FOREIGN KEY constraint L = log FN = scalar function IF = inline table function P = Store Process PK = PRIMARY KEY constraint (type is k) RF = copy Filter stored procedure S = system table TF = table function TR = trigger U = User Table UQ = UNIQUE constraint (type k) V = view X = Extended Stored Procedures*/ SELECT [name],[ID], Crdate fromsysobjectswhereXtype=' U '/*the xtype represents the parameter type, usually including the following c = CHECK constraint D = default value or
table, column exists, if not exist, createOne, the table does not exist to create:?
12345678910
if not exists (select * from sysobjects where id = object_id(‘mytab‘) and OBJECTPROPERTY(id, ‘IsUserTable‘) = 1)create table mytab(id int,age int ,name varchar(max),primary key (id,age))go
Second, the column does not exist to create.?
1
if notexists (select * from sys
Tags: --sql Server 2005, or 2012:select * from Information_schema.tables--sql server 2000:select * from sysobjects WHERE xt ype= ' u ' select * from sysobjects WHERE xtype= ' u ' select table_name from GEOVIDNU. Information_schema. Tablesselect sobjects.namefrom sysobjects
Pivot is used to rotate column values to column names (row to column), and SQL Server 2000 can be implemented with an aggregate function with a case statementThe general syntax for pivot is: Pivot (aggregate function (column) for
During database design, sometimes attribute record values belonging to the same person are stored in multiple records for the purpose of data standardization, you want to merge multiple attribute data into one row for display. This is a row-to-column conversion.
For example, the score table.
What is the efficiency of Row-to-column conversion? I tried the following three methods to perform a row-to-
First, backgroundThe ID field in table A in the SQL Server database is defined as: [ID] [int] IDENTITY, as the data grows, the ID value is close to 2147483647 (the value range for int is:-2 147 483 648 to 2 147 483 647). , although the old data has been archived, but the table needs to retain the most recent 100 million data, how to solve the ID value of the rapid explosion problem?There are two ways to sol
name+ '],' from#行数据中的需要转成列标题的数据--one more, get rid of it.IF(@sql Stitching Final column != "' ) BEGIN SET @sql Stitching Final column = @sql Stitching Final column + '$' SET @
'))Drop function [dbo]. [function name]7 getting user-created object informationSELECT [name],[id],crdate from sysobjects where xtype= ' U '8 determining whether a column existsif exists (SELECT * from syscolumns where id=object_id (' table name ') and name= ' column name ')ALTER TABLE table name drop column name9 Jud
100336 lines, we remember this number Another feature of statistics is that statistics are automatically created on query columns (non-indexed columns), as followsDuring query execution, a statistic named: _wa_sys_00000003_24e8431a is created automatically.This statistic is the statistic for the COL3 column, and you can find the number of rows that are greater than or equal to 2012-10-20. In
SUM( CaseWEEK when 'Monday' ThenINCOMEEND) as [Monday],SUM( CaseWEEK when 'Tuesday' ThenINCOMEEND) as [Tuesday],SUM( CaseWEEK when 'Wednesday' ThenINCOMEEND) as [Wednesday],SUM( CaseWEEK when 'Thursday' ThenINCOMEEND) as [Thursday],SUM( CaseWEEK when 'Friday' ThenINCOMEEND) as [Friday],SUM( CaseWEEK when 'Saturday' ThenINCOMEEND) as [Saturday],SUM( CaseWEEK when 'Sunday' ThenINCOMEEND) as [Sunday] fromWeek_incomeHowever, a simpler approach is provided in
', n ' language ', UNION all
SELECT N ' Zhang San ', N ' English ', 100
GO
SELECT * FROM [Testrows2columns]
(Figure 2: Sample Data)
(b) First, a static way to achieve row to column, the effect of 3 is shown:
--1: Static splicing row to column
SELECT [UserName],
SUM (case [Subject] when ' math ' then [Source] ELSE 0 END) as ' [Mat
output stuid=3)Stuid=4 .....Therefore, the Select Assignment statement in the dynamic row to column above can be interpreted as:The results from the back of the (select course from scores) are assigned to the course variable one at a time, and the operation after each copy is: string connectionSoSelect @sql [email protected] + ' max (case course if ' +course + ' then score else 0 End '+ ' + ' + ' + ', ' fr
The method used to determine whether a table or column does not exist in SQL Server.
1. How to determine whether a Column exists in the Table in SQL Server
First, I would like to share with you two methods for determining whether
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.