Solved

stored proc in sql server

Posted on 2009-05-18
4
191 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now