Murugan.com
Murugan Andezuthu Dharmaratnam

  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  PHP   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  



Stored Procedure Input Variables


Home  > MSSQL  > Stored Procedure Input Variables 
       
example or sample code of Input variable in stored procedure for beginner. 

Or input variable is used to pass value from an application to a stored procedure. here I will write a stored procedure which takes a variable sortorder and ascending or descending will be passed from application. 


There are two types of variables. local and global. Lets focus on local variables for now. 

-> U can name the variable how every u want, but the requirement is that the variable begins with an @ Symbol. 
@SortOrder 
@FirstName 
@LastName 

Now the example for the same. 

Start with creating a store procedure. 

Open Microsoft SQL Server Management Studio Express


-> Right Click on  Databases->YouDatabase->Programmability->Stored Procedures
-> Select new Stored Procedure
You would get a genearted template 
CREATE PROCEDURE  
	-- Add the parameters for the stored procedure here
	<@param1, sysname,="" @p1="">  = , 
	<@param2, sysname,="" @p2="">  = 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT <@param1, sysname,="" @p1="">, <@param2, sysname,="" @p2="">
END
GO


Lets Modify it 


CREATE PROCEDURE sp_test2
	-- Add the parameters for the stored procedure here
	@SortField varchar(10) = 'USERNAME' 
AS
DECLARE @SQLGeneratedString varchar(255)
BEGIN
	SELECT @SQLGeneratedString = 'SELECT * FROM USERS  order by ' + @SortField + ' asc'
	EXEC(@SQLGeneratedString)
END
GO

I have create a variable @SQLGeneratedString and used string operations to genearte the required ouput string withe variable
exec executed the geneatated sql string. 











index

create udl file to test sql connection to the sql server

Error: 18452 SQL Server 2005 "Login failed for user 'sa'. The user is not associated with a trusted SQL Server connection."

How to copy a table with data to one db to another in sql 2005

How to insert multiple records to a table using a single Insert query

How to insert values to multiple tables using a single insert query statement

How to replace a part of a string of a field using query in MSSQL

ms sql Add field to table using Alter Table

ms sql server 2005 express edition

MSSQL What is the use of COALESCE() function

System.Data.SqlClient.SqlError The backup set holds a backup of a database other than the existing

What are the different login modes in sql server

where is sql server 2005 files stored

Windows 7 SQL Server 2008 Express R2 Error Unable to connect from remote machine desktop

sql server error exception system.data.sqlclient.sqlerror the media set has 2 media families but only 1 are provided

Restore a database to another database The backup set holds a backup of a database other than the existing

Stored Procedure Introduction

How to execute a stored procedure on the sql server query analzer

Create Stored Procedure Example For beginner

stored procedure variable orderby

Stored Procedcure to Update Table

Stored Procedure Input Variables

not getting sql server properties on clicking udl file opening as text

Generic Stored Procedure To Return Get Data From Table With Paging

Single Stored Procedure Insert Update Delete

Stored Perocedure ROWCOUNT

Invalid use of a side effecting operator EXECUTE STRING within a function

SQL Query MAX Value column

MSSQL Select GroupBy

SQL Occurrence of character in a string

TSQL Functions

t sql string manipulation STRING BETWEEN TWO STRINGS

TSQL CHECK IF NULL

Generic Stored Procedure Insert Update Delete

Cannot call methods on nvarchar max



  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  PHP   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  

Copyright 2009 @ Murugan Andezuthu Dharmaratnam