Solved

stored proc in sql server

Posted on 2009-05-18
4
212 Views
Last Modified: 2012-05-07

I have a front end form where in a customer can enter

his cusotmerID or AddressLine1 or customer_type or his firstname or his social security number.
This data lets say is from single table :tbl_customer. Its not mandatory for him to enter all.
I want to create a stored procedure to search and display record according to his input

can it be shown.
0
Comment
Question by:dotnet0824
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 200 total points
ID: 24413637
CREATE PROCEDURE GetCustomerDetails
@ColumnName varchar(20) ,
@Value  nvarchar(100)
AS
BEGIN
    SET NOCOUNT ON
    DECLARE @nSQL nvarchar(2000)
    SELECT @nSQL  = ' SELECT * FROM tbl_Customer WHERE '+@ColumnName +' = @Value  '
    exec sp_ExecuteSQL @nSQL, N'@Value nvarchar(100) ', @Value
END
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 50 total points
ID: 24414115
One small addition to aneesh's post.  Put [] around the column name in the generated SQL.
CREATE PROCEDURE GetCustomerDetails
@ColumnName varchar(20) ,
@Value  nvarchar(100)
AS
BEGIN
    SET NOCOUNT ON
    DECLARE @nSQL nvarchar(2000)
    SELECT @nSQL  = ' SELECT * FROM tbl_Customer WHERE [' @ColumnName  '] = @Value  '
    exec sp_ExecuteSQL @nSQL, N'@Value nvarchar(100) ', @Value
END

Open in new window

0
 

Author Comment

by:dotnet0824
ID: 24415319
He can either either of those. any one can be entered or two can be entered or all can be entered

i suppose we need or statements in the query . hope i am clear
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24415341
"hope i am clear "

clear as mud.

Care to take another stab at it :).
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to show the last employee that updated a record 5 46
sql help 5 53
Not selecting duplicate data 6 53
ms sql + top 1 for each customer 3 50
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question