Annee (8/5/2013)
I am using SQL server 2005Here is the ddl:
CREATE TABLE [dbo].[AgencySupplierPartner](
[ClientName] [nvarchar](15) NOT NULL,
[VendorName] [nvarchar](40) NOT NULL,
[TaxID] [nchar](9) NOT NULL,
[AcceptStandard] [nvarchar](10) NULL,
[AcceptSummary] [nvarchar](10) NULL
CONSTRAINT [PK_AgencySupplierPartner] PRIMARY KEY CLUSTERED
(
[ClientName] ASC,
[TaxID] ASC
)
I would like to write a stored procedure to which I would pass 3 parameters ClientName, VendorName and InvoiceType as parameters.
While ClientName and VendorName are the column names 'InvoiceType' is a variable which could be one of the column names(AcceptStandard or AcceptSummary)
I am trying to put it together as a stored proc(see below), but it doesn't work, probably due to syntax error where I am plugging in @InvoiceType.
Can someone please help me fix it? Thanks!
CREATE PROCEDURE [dbo].[pr_GetVendorInfo]
(
@ClientName nVarChar(15),
@VendorName nVarChar(40),
@InvoiceType nVarChar(10)
)
AS
SELECT COUNT(*) FROM AgencySupplierPartner
WHERE ClientName = @ClientName AND VendorName = @VendorName AND @InvoiceType = 'true'
You can't switch out columns like that. What this is doing is finding rows where ClientName = [the value passed in] AND VendorName = [the value passed in] AND where the value you passed in for @InvoiceType = 'true'.
You need to use dynamic sql when you need to use the columns passed in. Be very careful here though because you start to open the possibility for sql injection.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/