Original: SQL Server queries all table names and rows of dataQuery all indicateSelectfromwhere xtype='u'select* from sys.tablesQuerying all table names and rows in the databaseSELECTA.name as [TABLE NAME], B.rows as [RECORD COUNT] fromsysobjects asaINNER JOINsysindexes asB ona.ID=b.idWHERE(A.type= 'u' ) and(B.
Document directory
Example
Stuff:
For xml path
References
Example
Yesterday I encountered an SQL Server problem: I need to write a stored procedure to process data in several tables. The problem is that I want to splice multiple rows in one column of a table into one row, for example, a table has two columns of data:
Category
Name
A
Quick Method for counting the number of rows in each table in SQL Server
This article mainly introduces how to calculate the number of rows in each table in SQL Server. This article does not use the traditional count () function b
. We'll use the Common Table Expression (CTE) and put the ' self ' Join query in it.With duplicates as (select distinct A.custid as customer_id from Customers2 a join customers2 B on A.custid Let ' s check which rows got deleted.SELECT * from Customers2 goScenario 2.b:delete all duplicate records but keep the first original oneLet ' s first truncate the CUSTOMERS2 table and add the same rows again.Truncate
We usually need to query the total number of rows in a table. Many people prefer to use select count (*) from table. if the table data is very large, such queries are time-consuming and resource-consuming.
In fact, there are two ways to quickly query the table data of SQL Server.
1. sp_spaceused: One of the columns is row
SQL Server converts rows to columns: for example, if a table contains only a small amount of data, you can use static SQL statements such as selectstuName and max (caseCoursewhen language thenscoreelse0end) as language, max (caseCoursewhen math thenscoreelse0end) as math, max (caseCoursewhen
Starting with SQL Server 2008, Microsoft to improve query efficiency and other reasons, right-click on the table when the pop-up menu does not "show all Rows", and "select the first 1000 rows" instead. This can sometimes bring us some inconvenience.To change the method: Open SQL
Tags: server greater than from BLE tab style not row divSQL SERVER queries data between rows 20th through 301. Query the first 20 rows of the ID, after the query to remove 20 records of the first 10 recordsSelect Top * from Tbbank
WHERE bankid not in (select Top BankID from Tbbank ORDER by BankID ASC)
2. Query the firs
Tags: blog http data IO issues CTI Management SQLJust changed SQL Server2008Soon, the feeling of running speed, editing tips are more than the 05 version of the promotion, but in the maintenance test system encountered a 05 of the problem: 05 in the "Open Table" can edit all data rows, 08 to "Open the top 1000 lines" and "edit the Top 200 lines": The test system of data there are thousands of lines , how to
Each time you import an Excel table to the database, several more rows are displayed as 'null' rows. The following prompt is displayed when you delete the table.
"The key column information is insufficient or incorrect. Update affects multiple rows! "
Solution: You can see from your description that you have imported data from Excel to
SQL Server deduplication is one of our most common actions, and here's a look at six different ways SQL Server can remove duplicate rows for your reference.1. If there is an ID field, it is a unique fielddelect table TableName where ID not in (the Select Max (ID) from the ta
For repeated row deletion problems, it is difficult to find a suitable answer on the Internet and there are a lot of questions, but there is no solution to the previous record in the search engine.
In fact, this problem can be effectively solved.
1. If the table does not have a primary key (or the same row does not have different content columns), you need to create an auto-increment column to distinguish different columns. For example
Copy codeThe Code is as follows: alter table [tablename]
For repeated row deletion problems, it is difficult to find a suitable answer on the Internet and there are a lot of questions, but there is no solution to the previous record in the search engine.In fact, this problem can be effectively solved.1. If the table does not have a primary key (or the same row does not have different content columns), you need to create an auto-increment column to distinguish different columns. For exampleCopy codeThe Code is as follows:Alter table [tablename] add [TI
Deleting duplicate rows on SQL Server is one of the most common operations. The following describes six methods that can be used to delete duplicate rows on SQL Server for your reference.
1. If an ID field exists, it is a unique f
Tags: strong SP on BS SQL table nbsp Server cPre-Preparation:CREATE TABLE Nums (X int);CREATE TABLE T (X int);GoPurpose: Insert the table Nums | Delete | The update is reflected in the T table.--------------------------------------------------------------------------------------------------------------- ------------------------------Insert:Insert into Nums (X)Output inserted. X #记得它在列表之后Into T (X)VALUES (1)
Query all indicates that the select name from sysobjects where xtype= ' u ' select * from sys.tables//queries the database for all table names and row counts select A.name, B.rowsfrom syso Bjects as a INNER JOIN sysindexes as B on a.id = b.idwhere (A.type = ' u ') and [B.indid in (0, 1)] ORDER by A.name,b.rows DE sc//query all marked and space consumption \ line number Selectobject_name (ID) tablename,8*reserved/1024 Reserved,rtrim (8*dpages) + ' KB ' used,8* ( reserved-dpages)/1024 unused,8*dpa
to judge repetition. For example, if only col1 is used, if the content of col1 is the same, the record is the same.
5.Copy codeThe Code is as follows: select identity (int, 1, 1) as id, * into # temp from tabelSelect * from # temp where id in (Select max (id) from # emp where having count (*)> 1 group by col1, col2, col3 ...)
6.Copy codeThe Code is as follows: select distinct * into # temp from tablenameDelete tablenameGoInsert tablename select * from # temp SqlclubGoDrop table # temp
The prece
and from this table column: Keywords through a derived table connection to uf_splitstringtotable (filehistory.keywords). The specific code is as follows: SELECTt1.[Id], T1.[Keywords], T1.[FileName], T1.[fileextension], T1.[Createduser], T1.[Createdtime], T1.[Importtype], i.item as keyword fromfilehistory T1OUTERAPPLY uf_splitstringtotable (t1. Keywords,',') I After the code executes successfully, the following results are returned: In this way, we can finish the results we need. 2015,fighting
1. If an ID field exists, it is a unique field.Copy codeThe Code is as follows:Delect table where id not in (Select max (id) from table group by col1, col2, col3...) The field followed by the group by clause is the condition for you to judge repetition. For example, if only col1 is used, if the content of col1 is the same, the record is the same.2. This can also be used to determine all fields.Copy codeThe Code is as follows:Select * into # aa from table group by id1, id2 ,....Delete tableInsert
Where has count (*) >1
GROUP BY Col1,col2,col3,col4
)
The field followed by the GROUP BY clause is the condition that you use to determine the repetition, such as only col1, so as long as the content of the Col1 field is the same as that of the record.
5.
Copy Code code as follows:
Select Identity (int,1,1) as id,* into #temp from tabel
SELECT * from #temp where ID in (
Select Max (ID) from #emp where has count (*) >1 GROUP by col1,col2,col3 ...)
6.
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.