An Idea can change your life.....
Showing posts with label sql server samples. Show all posts
Showing posts with label sql server samples. Show all posts

Saturday, February 06, 2010

Delete Duplicate Records

___________________________________________________________


CREATE TABLE dbo.car_info
(
Car_Sl_No INT NOT NULL,
CarCompany NVARCHAR (max) NULL,
CarBodyType NVARCHAR (max) NULL,
CarName NVARCHAR (max) NULL,
EngineType NVARCHAR (max) NULL
)
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)VALUES (1, 'Maruti ', 'small ', 'Maruti-800 ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (2, 'Honda ', 'sedan ', 'City ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (3, 'Maruti ', 'small ', 'Maruti-800 ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (4, 'Maruti ', 'small ', 'Waganor Duo ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (5, 'Honda ', 'sedan ', 'City ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (6, 'TATA ', 'small ', 'indica ', 'diesel')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (7, 'Mahindra ', 'SUV ', 'Scorpio ', 'diesel')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (8, 'TATA ', 'SUV ', 'Sumo ', 'diesel')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (9, 'Maruti ', 'sedan ', 'SX4 ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (10, 'Maruti ', 'sedan ', 'Swift-Dzire ', 'diesel')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (11, 'TATA ', 'small ', 'Nano ', 'petrol')
GO
______________________________________________________________________________

Delete duplicate records based on the company name only
delete from dbo.car_info
where exists
(select Car_Sl_No from dbo.car_info s
where s.CarCompany = dbo.car_info.CarCompany and s.Car_Sl_No < dbo.car_info.Car_Sl_No)

Wednesday, November 04, 2009

Sub Query

















The SQL subquery feature lets you use the results of one query as part of another query.

A subquery is a query within a query.



A sub-query is a SQL Server statement embedded inside of another SQL Server statement.

Sub-query execution is dependent upon the nesting level of the query. The execution tree goes from inner-most queries to outer-most queries. The higher nested queries can access the results returned by the lower nested queries.


Paranthesis
  • A sub‐query must be enclosed in the parenthesis
comparison operator
  • A sub‐query must be put in the right hand of the comparison operator
Order By
  • A sub‐query cannot contain an ORDER‐BY clause
Number Of SubQuery's
  • A query can contain more than one sub‐query
Types
  • Sub Query
  • Correlated Sub Query
Results
  • Single Row
  • Multipe Row
  • Multiple Columns

Correlated Sub Query Sample
A correlated sub-query is dependent upon the outer query. The outer query and the sub-query are related typically through a WHERE statement located in the sub-query. The way a correlated sub-query works is when a reference to the outer query is found in the sub-query, the outer query will be executed and the results returned to the sub-query. The sub-query is executed for every row that is selected by the outer query.
____________________________________________________
CREATE TABLE SalesHistory

(
SaleID INT IDENTITY(1,1),
Product VARCHAR(30),
SaleDate SMALLDATETIME,
SalePrice MONEY
)

SELECT SaleDate, Product, SalePrice,
ISNULL(
(
SELECT
SUM(SalePrice)
FROM
SalesHistory sh1
WHERE
Sh1.Product = sh.Product AND
Sh1.SaleID < sh.SaleID
),0) AS RunningTotal
FROM SalesHistory sh
ORDER BY Product, SaleID

____________________________________________________

Sunday, November 30, 2008

sql server insert multiple records


CREATE
TABLE #CALCULATE
(
COL1 VARCHAR(20),

COL2 VARCHAR(20))

Using Insert INTO commands for each row

INSERT INTO #CALCULATE
(COL1,
COL2)
VALUES ('one',
1);
INSERT INTO #CALCULATE
(COL1,
COL2)
VALUES ('two',
2);
INSERT INTO #CALCULATE
(COL1,
COL2)
VALUES ('three',
3);
INSERT INTO #CALCULATE
(COL1,
COL2)
VALUES ('four',
4);
INSERT INTO #CALCULATE
(COL1,
COL2)
VALUES ('five',
5);

SELECT
*
FROM #CALCULATE

DELETE FROM #CALCULATE

Using Union All command

INSERT
INTO #CALCULATE
(COL1,
COL2)
SELECT 'one',
1
UNION ALL
SELECT 'two',
2
UNION ALL
SELECT 'three',
3
UNION ALL
SELECT 'four',
4
UNION ALL
SELECT 'five',
5

SELECT
*
FROM #CALCULATE

CREATE TABLE #CALCULATETEMP
(
COL1 VARCHAR(20),

COL2 VARCHAR(20))

Insert from another table

INSERT
INTO #CALCULATETEMP
(COL1,
COL2)

SELECT COL1,
COL2
FROM #CALCULATE

Create table and insert the data from another table

SELECT
*
FROM #CALCULATETEMP

SELECT COL1,
COL2
INTO #CALCULATETEMP1
FROM #CALCULATE

SELECT *
FROM #CALCULATETEMP1

DROP TABLE #CALCULATE

DROP TABLE #CALCULATETEMP

DROP TABLE #CALCULATETEMP1

Reference : www.sqlauthority.com

sql server execute all files in a folder using sqlcmd

Step 1. Create a sql file (proc.sql) and palace it here C:\SqlFiles\01_database\proc.sql

create procedure procName
as
begin
select * from DimProduct
end

Step2. Create a sql file(proc1.sql) and place it here C :\SqlFiles\proc1.sql

create procedure procName1
as
begin
select * from DimProduct
end

step3.Create a BatchCommand file(ExecuteSqlFiles.bat) with following text in it and place it here C:\SqlFiles\ExecuteSqlFiles.bat

for /R %%X in (*.SQL) do SQLCMD -S Server -d Database -U UserID -P Password -I -i "%%X" >> ResultScript.txt

Step4. Run the ExecuteSqlFiles.bat (i.e by clicking on it)

A file (C:\SqlFiles\ResultScript.txt) if not exist is created and the errors encounterd while executing the sql files is written to the file.

Wednesday, July 30, 2008

Tuesday, February 12, 2008

Database Design principals & Normalization

Sample script to learn about database design principals.
1.Normalization concepts
2.creating database.

Normalization Database

Normalization queries

Monday, February 11, 2008

sql server new features

Sample script for
  1. using comman type expression (CTE)
  2. Pivot
  3. cross apply
  4. row_number
  5. rank
  6. dense_rank
  7. top
  8. Try/Catch Error Handling
  9. FOR XML PATH
  10. view
  11. function
sql server new features

sql server selecting data

Sample script for selecting data. selecting data

Sql server Samples

Script for creating Northwind Database. Northwind

Script for creating Pubs Database. Pubs