How to know SQL Server version


select @@servernameASServerName

Select @@VERSIONAsSQLServerVerjion

go

selectserverproperty(‘Edition’)AsServerEdition

selectserverproperty(‘ProductLevel’)ServerProductLevel

selectserverproperty(‘BuildClrVersion’)BuildVersion

Bulk insert In SQL Server


/*

Bulk insert And OPENROWEST

*/

http://msdn.microsoft.com/en-us/library/aa225968(v=sql.80).aspx

—————————*******************Bulk Insert******************—————————–

–Step 1 Create Table [CSVTest]

CREATE

TABLE [dbo].[CSVTest]

(

[ID] [int]

NULL,

[FirstName] [varchar]

(40)COLLATE Arabic_CI_AS NULL,

[LastName] [varchar]

(40)COLLATE Arabic_CI_AS NULL,

[nvarchar]

(50)COLLATE Arabic_CI_AS NULL

—Step 2 Create Text File on D:\ Location

—Step 3 Try Bulk Insert

go

BULK

INSERT

[CSVTest]

FROM

‘D:\TxtFile2.txt’

WITH

(

BATCHSIZE

= 2,—- Show The Rows Affected

FIRSTROW

= 2,—– Start the insert from Row number 2

FIELDTERMINATOR

=‘,’,

ROWTERMINATOR

=‘\n’,

LASTROW

= 4,—- End the insert when you Arraive to row number 4

MAXERRORS

= 1 — Number of errors After this Number The SQL not complete the insert the defult value is 10

)

GO

–Step No 4 Check the Data inserted

Select

*from [CSVTest]

Delete

from [CSVTest]

Drop

Table [CSVTest]

How to find a table when you don’t know this table in any database?


DECLARE @table sysname, @SQL NVARCHAR(MAX)
SET @table = ‘Table_Name’ —– Replace Table_Name by your table name you want to find it
SET @SQL = ”
SELECT @SQL = @SQL + ‘;
IF EXISTS (SELECT 1 from ‘ + QUOTENAME(name) + ‘.INFORMATION_SCHEMA.Tables WHERE Table_Name = @table
AND TABLE_TYPE = ”BASE TABLE”)

PRINT ”Table ‘ + @table + ‘ found in ‘ + name + ””

FROM sys.databases

EXECUTE sp_executeSQL @SQL, N’@table sysname’, @table

Convert Row To Column


We can Convert row to column by Twoways (Select Case – Pivot) let’s see How can i make this

Way No One

—Create Table

create Table States
(
id int primary key identity,
City nvarchar (20),
PersonName nvarchar (20)
)

—-Insert Data

insert into States values (‘Egypt’,’MOstafa’)
insert into States values (‘Egypt’,’Sayed’)
insert into States values (‘Egypt’,’Refay’)
insert into States values (‘London’,’Jon’)
insert into States values (‘London’,’Better’)
insert into States values (‘London’,’Cristian’)

—–Convert row to column

SELECT dbo.States.id,
(CASE City WHEN ‘Egypt’ THEN
(PersonName )  ELSE ‘No Name’ END) AS Egypt
,(CASE City WHEN ‘London’ THEN
(PersonName)ELSE ‘No Name’END) AS London
from States

Way No Two

—Create Table

CREATE TABLE [dbo].[Visit_Count_City](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[city] [nvarchar](50) NULL,
[VisitCount] [bigint] NULL
)

—–Insert Data

INSERT INTO visit_count_city VALUES (‘Mostafa’,’Sudia Arabia’,2)
INSERT INTO visit_count_city VALUES (‘Mostafa’,’Egypt’,2)
INSERT INTO visit_count_city VALUES (‘Mostafa’,’India’,2)
INSERT INTO visit_count_city VALUES (‘syaed’,’Sudia Arabia’,2)
INSERT INTO visit_count_city VALUES (‘Ahmed’,’Quter’,2)
INSERT INTO visit_count_city VALUES (‘Ali’,’Egypt’,2)
INSERT INTO visit_count_city VALUES (‘Mohamed’,’India’,2)

—-Convert row to column

SELECT * FROM
( SELECT p.NAME,p.city,p.VisitCount FROM dbo.Visit_Count_City AS p)datatable
PIVOT
(
SUM (VisitCount)
FOR city IN
(Egypt,[Sudia Arabia],Quter,India))PivotTable
go

Delete files in your Pc by SQL Server


If you have program write his log in files in your PC and you want to delete this file by your program you can make job in sql server to do this operation by ‘Ole Automation Procedures’ in SQL Server

in the first we need to enable ‘Ole Automation Procedures’ usind sp_configure

exec sp_configure ‘Ole Automation Procedures’, 1
go
reconfigure
go

After enabling it we can delete the files with Ole Automation Procedures which using FSO (File System Object) from SQL Server and we need to pass method name ‘DeleteFile’ in the tsql script. You can delete all the files or specific files as well. you can use this code to delete all temp in your drive

DECLARE

@Result int

DECLARE

@FSO_Token int

EXEC

@Result =sp_OACreate‘Scripting.FileSystemObject’, @FSO_Token OUTPUT

EXEC

@Result =sp_OAMethod@FSO_Token,‘DeleteFile’,NULL,‘D:\TestFolder\*.txt’

EXEC

@Result =sp_OADestroy@FSO_Token