How to find highest 3 salary in sql?
Select top 1 sal from tbl where sal in(select top 3 sal from tbl order by sal desc) order by sal asc.
How to find Duplicate entries in sql?
Select distinct(sal) from tbl group by sal having count(*)>1;
How to delete Duplicate entries in sql?
Delete from tbl where id in ( select distinct(id) from tbl group by id having count(id)>1);
How to insert row number in sql query?
Select row_number() over(order by id),* from table;
How we can use try catch in SQL?
Begin try
Begin transaction
--------
--------
Commit transaction
End try
Begin catch
Rollback transaction
End catch
How to shows TCP details of SQL Server?
Select * from sys.tcp_endpoints;
How to get all tables and columns within a database?
Select * from sysobjects;
Select * from syscolumns;
Select * from systypes;
How to insert more than one record in a table?
Insert into db2.dbo.tbl(id,name) select id,name from db1.dbo.tbl;
How to add unique key constraints after create a table?
Alter table tblname add unique(colname);
Search You Want
Monthwise Posts
Search Under Category
Posts’ Tags
.Net (223) ADO.NET (10) Air Conditioner (16) Asp.Net (137) Body-Weight (9) C# (39) C Programming Language (23) Data Structure (36) Design Pattern (20) Digital Currency (9) Eating & Drinking (55) Exam 70-511 - TS: Windows Applications Development with Microsoft .NET Framework 4 (18) Exam 70-536 - TS: Microsoft .NET Framework - Application Development Foundation (15) Exam 70-562 - TS: Microsoft .NET Framework 3.5, ASP.NET Application Development (35) Fever (16) Finance (47) Funds (58) General Knowledge (128) GST (11) Insurance (25) Internet Information Services (IIS) (13) Interview Questions and Answers (471) Investment (21) JavaScript (19) Job (60) LINQ (Language Integrated Query) (56) MCPD Web Developer 4.0 Certification (35) Mental Health (13) MVC (156) OOPs (73) Pain (13) PPF (22) Shares (21) Silverlight (12) Skin (22) Software Development Methodology (12) Software Testing (32) SQL (113) TAX (25) TDS (12) Trading (16) Treatment (14) Web-Service (10) Windows Azure (11) Windows Communication Foundation (WCF) (49)
Your script to delete duplicates will delete all rows, instead of leaving behind one unique row. Try this instead:
WITH CTE_RankedIds AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY [id] ORDER BY @@SPID) as [rank],
[id]
FROM [dbo].[TestData]
)
DELETE FROM CTE_RankedIds
WHERE [rank] > 1
Don’t use sysobjects, syscolumns or systypes anymore. They’ve been deprecated for quite some time. Instead use [sys].[objects], [sys].[columns] and [sys].[types]
SELECT
[sch].[name] as [schema_name],
[tbl].[name] as [table_name],
[col].[name] as [column_name],
[typ].[name] as [type_name],
[col].[max_length] as [max_length],
[col].[precision] as [precision],
[col].[scale] as [scale],
[col].[is_nullable] as [nullable]
FROM
[sys].[schemas] [sch]
INNER JOIN [sys].[tables] [tbl] ON [sch].[schema_id] = [tbl].[schema_id]
INNER JOIN [sys].[columns] [col] ON [tbl].[object_id] = [col].[object_id]
INNER JOIN [sys].[types] [typ] ON [col].[user_type_id] = [typ].[user_type_id]
ORDER BY
[sch].[name],
[tbl].[name],
[col].[column_id]
Your script to delete duplicates will delete all rows, instead of leaving behind one unique row. Try this instead:
[code]
WITH CTE_RankedIds AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY [id] ORDER BY @@SPID) as [rank],
[id]
FROM [dbo].[TestData]
)
DELETE FROM CTE_RankedIds
WHERE [rank] > 1
[/code]
Don’t use sysobjects, syscolumns or systypes anymore. They’ve been deprecated for quite some time. Instead use [sys].[objects], [sys].[columns] and [sys].[types]
[code]
SELECT
[sch].[name] as [schema_name],
[tbl].[name] as [table_name],
[col].[name] as [column_name],
[typ].[name] as [type_name],
[col].[max_length] as [max_length],
[col].[precision] as [precision],
[col].[scale] as [scale],
[col].[is_nullable] as [nullable]
FROM
[sys].[schemas] [sch]
INNER JOIN [sys].[tables] [tbl] ON [sch].[schema_id] = [tbl].[schema_id]
INNER JOIN [sys].[columns] [col] ON [tbl].[object_id] = [col].[object_id]
INNER JOIN [sys].[types] [typ] ON [col].[user_type_id] = [typ].[user_type_id]
ORDER BY
[sch].[name],
[tbl].[name],
[col].[column_id]
[/code]