0% found this document useful (0 votes)
237 views14 pages

Ms SQL Notes

The document provides SQL commands and examples for creating tables, inserting and updating records, joining tables, using aggregate functions, modifying database files, and using cursors. Key points covered include creating and altering tables, performing CRUD operations on records, joining tables, using SUM to aggregate data, adding and removing database files, and examples of using cursors to iterate through records from multiple tables.

Uploaded by

vitu3075
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
237 views14 pages

Ms SQL Notes

The document provides SQL commands and examples for creating tables, inserting and updating records, joining tables, using aggregate functions, modifying database files, and using cursors. Key points covered include creating and altering tables, performing CRUD operations on records, joining tables, using SUM to aggregate data, adding and removing database files, and examples of using cursors to iterate through records from multiple tables.

Uploaded by

vitu3075
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 14

Create Table

Create Table [Employee] ( Id NAME Float NOT NULL PRIMARY KEY,

nVARCHAR(50)

NOT NULL, NOT NULL,

DEPARTMENT SALARY CITY )

nVARCHAR(20) Float

NOT NULL, NOT NULL,

nVARCHAR(20)

[Run Command for help F5]


SELECT * FROM [Employee]

Enter The Value


INSERT INTO Employee (ID,NAME,DEPARTMENT,SALARY,CITY) VALUES (1,'ABC','CHECKING',1500,'LDH') INSERT INTO Empolyee (ID,NAME,DEPARTMENT,SALARY,CITY) VALUES (3,'JKL','FINISHING',1000,'JAL')

Delete Value
Delete From Employee Where [Name]=abc

Update Values
Update [Employee] Set [Name]=Lucky where [Employee Id]=2

Record Find
c

Add Column

Alter Table [Employee]

Add [D O B] SmallDateTime

Delete Column
Alter Table [Employee]

Drop column [Remarks]

-------------------------------------------------------------------------------------------------------------------

Column Name Change


[Employee Id] as [Emp Id] (after change Column name is [Emp Id] ) -----------------------------------------------------------------------------------------------------------------------------

Join Two Or Multipal Table


(first function)

(for help inner join table)

Select [Employee].[Emp id],[Employee].[Emp Name], [Employee Account]. [Salary] From [Employee] (first Table)

Inner Join [Employee Account] (Second Table) On [Employee].[Emp Id]=[Employee account].[Emp id] (column its same ,because give primary key)

Join Two Or Multipal Table


(second Function)

(for help inner join table)

Select [Employee].[Emp id],[Employee].[Emp Name], [Employee Account]. [Salary] From [Employee](first Table) , [Employee Account] (Second Table) where [Employee].[Emp Id]=[Employee account].[Emp id] (column its same ,because give primary key)

FUNCTION OF CTL+ALT+SHFT+F12 IN EMANAGE


It is used to open a structure of that page.it is used in emanage to open a structure of any view in emanage.exp.purchase view, sale view etc

Use of key word [order by]

While joining any two table and to see a table in sequence order we use a key word order by .by this our entries in table will appear in an order..
Exp: Select [Employee].[Emp id],[Employee].[Emp Name], [Employee Account]. [Salary] From [Employee] (first Table),

[Employee Account] (Second Table) Where [Employee].[Emp Id]=[Employee account].[Emp id] Order by [employee].[emp id]

SUM () FUNCTION
The SUM() function returns the total sum of a numeric column. EXP: Select SUM(AMOUNT) FROM [SALE DETAIL}

DB-TABLE NAME
DB STANDS FOR DATABASE ,DB-TABLE NAME IS A DATABASE TABLE NAME IN WHICH OUR ENTRIES ARE STORED ..FOR A PARTICULAR TABLE ENTRY.AND EASY IN FINDING ENTRY IN DATABASE

CURSOR _STATUS COMMAND


EXP: DECLARE @SA_BILLNO NVARCHAR(200), @SA_BILLDATE SMALLDATETIME

DECLARE SALE_BILL CURSOR FOR SELECT BILLNO, BILLDATE FROM SALE ORDER BY BILLDATE

OPEN SALE_BILL

-- Perform the first fetch and store the values in variables. -- Note: The variables are in the same order as the columns -- in the SELECT statement.

FETCH NEXT FROM SALE_BILL INTO @SA_BILLNO, @SA_BILLDATE

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.

WHILE @@FETCH_STATUS = 0 BEGIN

-- Concatenate and display the current values in the variables.

PRINT 'BILL: ' + @SA_BILLNO+' '+ CAST(@SA_BILLDATE AS NVARCHAR(200))

-- This is executed as long as the previous fetch succeeds.

FETCH NEXT FROM SALE_BILL INTO @SA_BILLNO, @SA_BILLDATE END

CLOSE SALE_BILL DEALLOCATE SALE_BILL

UNION COMMAND
IT COMBINES DIFFERENT COLUMNS INTO ONE COLUMN ONLY.

EXP: select [Column name] from [table] union select [column name] from [table] union select [column name] from [table]

SCROLL CURSOR COMMAND


EXP: --DECLARE @SA_BILLNO NVARCHAR(200), @SA_BILLDATE SMALLDATETIME

DECLARE SALE_BILL SCROLL CURSOR FOR SELECT BILLNO,BILLDATE FROM SALE

ORDER BY BILLDATE

OPEN SALE_BILL

-- Fetch the last row in the cursor. FETCH LAST FROM Sale_Bill

-- Fetch the row immediately prior to the current row in the cursor. FETCH PRIOR FROM Sale_Bill

-- Fetch the second row in the cursor. FETCH ABSOLUTE 2 FROM Sale_Bill

-- Fetch the row that is three rows after the current row. FETCH RELATIVE 3 FROM Sale_Bill

-- Fetch the row that is two rows prior to the current row. FETCH RELATIVE -2 FROM Sale_Bill

CLOSE SALE_BILL DEALLOCATE SALE_BILL

Power Command
Power command needs numeric value to show its result

Exp: DECLARE @value int, @counter int

SET @value = 2 SET @counter = 1 --checking status WHILE @counter < 5 BEGIN

--executes according to the value SELECT POWER(@value, @counter) SET NOCOUNT ON SET @counter = @counter + 1 SET NOCOUNT OFF END

Database creating command


Create a new database Exp: -------------DATABASE CREATING COMMAND------------------------------------------------------create database kun11

Adding File To Database


It adds files in a database

(single file)

-----------------ADDING FILE TO DATABASE----------------------------------------------------

Alter Database kun11 add file ( name = kun11dat2, Filename = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\kun11dat2.ndf', size = 5MB, maxsize =50MB, filegrowth = 5MB )

Adding File To Database File)


----------------FOR ADDING NUMBER OF FILES IN DATABASE----------------------------------------

(More Than One

Alter Database kun11 ADD file ( name = kun11dat2, Filename = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\kun11dat2.ndf', size = 5MB, maxsize =50MB, filegrowth = 5MB ),

( name = kun11dat3, Filename = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\kun11dat3.ndf', size = 5MB, maxsize =50MB, filegrowth = 5MB )

Modifying a file in database

---------------------TO MODIFY A FILE IN DATABASE--------------------------------------------Alter database KUN11FG1 MODIFY FILEGROUP [ KUN11FG1] DEFAULT

Removing a file from database


---------------------TO REMOVE A FILE FROM DATABASE------------------------------------------ALTER DATABASE KUN11 REMOVE FILE KUN11dat2

HOW TO REMOVE EXISTING VALUES FORM TABLE

It deletes the existing alues which are already present in a table. DELETE FROM [TABLE NAME] Exp: delete from [temp sale]

To fetch more than one table by cursor function

Exp: DECLARE @SaleType NVARCHAR(200),@BILLNO NVARCHAR(200), @BILLDATE SMALLDATETIME, @CHALLANNO FLOAT,@PARTYCODE NVARCHAR(200),@SALECODE NVARCHAR(200),@productcode nvarchar(200), @productname nvarchar(200),@units nvarchar(200),@amount float

DECLARE SALE_BILL CURSOR FOR SELECT [SALE TYPE],[BILLNO],[BILLDATE],[CHALLAN NO],[PARTYCODE], [SALECODE] FROM SALE ORDER BY BILLDATE

OPEN SALE_BILL

FETCH NEXT FROM SALE_BILL INTO @SALETYPE,@BILLNO, @BILLDATE,@CHALLANNO,@PARTYCODE,@SALECODE

WHILE @@FETCH_STATUS = 0 BEGIN

PRINT 'SALE: ' + @SALETYPE+@BILLNO+' '+ CAST(@BILLDATE AS NVARCHAR(200))+ CAST(@CHALLANNO AS NVARCHAR(200)) +@PARTYCODE+@SALECODE

declare saledetail_bill cursor for select [product code],[product name],[units],[amount] from [sale],[sale detail] where [sale].[sale type]=[sale detail].[sale type] and [sale].[billno]=[sale detail].[billno]

open saledetail_bill

fetch next from saledetail_bill into @productcode,@productname,@units,@amount while @@fetch_status = 0 begin

print 'saledetail: ' +@productcode+@productname+ ''+cast(@units AS NVARCHAR(200))+ cast(@amount AS NVARCHAR(200))

fetch next from saledetail_bill into @productcode,@productname,@units,@amount

END CLOSE saledetail_BILL DEALLOCATE saledetail_BILL FETCH NEXT FROM SALE_BILL INTO @SALETYPE,@BILLNO, @BILLDATE,@CHALLANNO,@PARTYCODE,@SALECODE

END CLOSE SALE_BILL DEALLOCATE SALE_BILL

Group by:
It is used when we want to group each item of same kind only once. Syntax: select [column name1], sum(amount) from [table] group by [column name1]

Exp: 1.select [product code],sum(amount) as TAmt from [purchase detail] group by [product code]

2. select [product code],[product name], sum(amount) as TAmt from [purchase detail] group by [product code] Now it will show an error message [Column 'purchase detail.Product Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.] now select [product code],[product name], sum(amount) as TAmt from [purchase detail] group by [product code] ,[product name]

Having
Having clause works same as where condition do

Syntax: SELECT select_list [ INTO new_table ] FROM table_source [ WHERE search_condition ] [ GROUP BY group_by_expression ] [ HAVING search_condition ]

You might also like