Tuesday 25 June 2013

SQL Server Stored Prcoedure

A stored procedure is a precompiled group of Transact-SQL statements that has been created and stored in the database. 
Benefits:
1)  Modular programming
2) Performance - Faster Execution and Reduced network traffic and Improved Security.
 

-- to create a new Store Procedure

CREATE PROCEDURE Procedure_name
@Username varchar(100)
AS
BEGIN
SELECT Username FROM USER_table WHERE USERNAME=@Username
END
·        Finally select all text and press F5

-- to ALTER or Modify an already existing Store Procedure

ALTER PROCEDURE Procedure_name
@Username varchar(100)
AS
BEGIN
SELECT Username FROM USER_table WHERE USERNAME=@Username
END
·        Finally select all text and press F5

--to view the stored procedure code
sp_helptext proc_name

--to execute the stored procedure code
EXEC Procedure_name  ‘argu1’,’argu2’,….
Or
Procedure_name  ‘argu1’,’argu2’,….

--to delete stored procedure from db
drop procedure [proc_name]
or
drop proc [proc_name]


Examples:

Branching within Stored Procedure

Create proc [dbo].[proc_name]
@domain nvarchar(100),
@location nvarchar(100) = ''
as
begin
IF(@domain = 'yahoo.com') --or (@domain = 'gmail.com')
BEGIN

SELECT DomainName= SUBSTRING(DomainName,CHARINDEX('@',DomainName)+1,50),ApproversEmailId,ApproversName, BankName   FROM InternalBank_Details
WHERE DomainName = 'test@' + @domain AND Location = @location

END

ELSE

BEGIN

SELECT DomainName= SUBSTRING(DomainName,CHARINDEX('@',DomainName)+1,50),ApproversEmailId,ApproversName  FROM OutBank_Details WHERE DomainName = 'test@' + @domain

END

end

SQL Server Basic Queries

--copy all data from another table and the format of the table(columns)
select * into new_table from old_table

--delete all rows in a table
truncate table table_name

--delete table from the db
drop table table_name

--copy onlyy the format of the table(columns) and its data types
select top 0 * into new_table from old_table

--to create a new table
create table persons
(
personid int,
lastname varchar(255),
firstname varchar(255),
address varchar(255),
city varchar(255)
);

--to insert a row or value in a table
insert into persons (personid, lastname, firstname, address, city,)
values (1,'tom b. erichsen','skagen ‘,'stavanger 4006 st','norway');

--to update already existing row or value in a table
update persons set lastname ='alfred schmidt', city='london' where personid = 1

--to delete a particular row in a table (use where condition)
delete from persons where where personid = 1and city ='norway'

--to delete all row in a table (use where condition)
delete from persons
or

delete * from persons

Saturday 8 June 2013

Site pages and Application pages

Application Pages:

These pages are normal asp.Net pages and they will reside on the actual file system disk (%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS directory) and exist for every site in a Web application.

This folder is mapped to an Internet Information Services (IIS) virtual directory called _layouts. Every site and sub site will have access to the application pages by using the _layouts virtual directory. 
For example, 
http://spdemo2010/_layouts/ viewlsts.aspx and http://spdemo2010subsite/_layouts/settings.aspx access the same application page on the front-end Web server unlike site pages, which are an instance for the specified site.

Site pages:

Site pages can be provisioned to the file system, but their content lives in the content database. Site pages types
·         Standard page –It contains text, images, Web Parts, and other elements.
·         Web Parts page- It contains Web Parts in Web Part zones.
Both types of site pages are edited using a Web browser or Microsoft SharePoint Designer.

Site pages are provisioned from a template page that is stored on the file system of the front-end Web server. When a site is provisioned, SharePoint Foundation creates a pointer to the instance of the page template on the file system. This allows SharePoint Foundation to avoid repeatedly creating copies of the pages, which are provisioned each time a site is created.

Differences between both:

·         Application pages have no content of their own
·         Application pages cannot be edited in Designer or the browser.
·         Application pages are visible under every site in your farm.
·         Application pages are mainly used for configuration pages
·         Both Application and Site pages can use master pages. Application pages get a "dynamicmasterpagefile" property instead of a "masterpagefile" on their @page directive.




Ghosting and Unghosting in Sharepoint 2010


Ghosted pages:

Pages whose content doesn’t reside in the content database (save as a row with a null value for Content) and they will reside on the actual file system disk. (If the page is not customized, server-side code is supported on the page.)

UnGhosted pages:

Site pages can be customized and if they are customized, their content will be stored in the content database. (These pages can affect the performance of site and , server-side code does not run, and the page does not render. This includes the code-behind for the page itself.)         

To enable/disable ghosting, you can do the action in Central Administration

General Application Settings > SharePoint Designer > uncheck ‘Enable Detaching  Pages from the Site definition’.

So if you uncheck (disable) the above option, sharepoint designer doesn't allow you to customize site pages...thus avoid unghosting. 

Happy Sharepointing. :)