Executing SQL Function Using Linq - c#

I have created a function in MSSQL 2005 and I want to run this function using linq. My function has 2 parameters. The definition is seen below:
USE [MobileGateway]
GO
/****** Object: UserDefinedFunction [dbo].[Fn_GetNoLocationAddress] Script Date: 11/07/2012 08:27:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Fn_GetNoLocationAddress]
(
-- Add the parameters for the function here
#Site nvarchar(255),
#ReceivedDate int
)
RETURNS #noLocationAddress TABLE (
RequestID int NOT NULL,
Barcode varchar(50) NOT NULL,
AdrID int NOT NULL,
Name varchar(50) NOT NULL,
Street varchar(50) NOT NULL,
HouseNo varchar(4) NOT NULL,
Postal varchar(8) NOT NULL,
City varchar(50) NOT NULL,
Country varchar(50) NOT NULL,
Latitude varchar(50) NOT NULL,
Longitude varchar(50) NOT NULL,
ReveivedDate datetime NOT NULL
)
AS
BEGIN
-- Add the SELECT statement with parameter references here
INSERT INTO #noLocationAddress
SELECT TOP (100) PERCENT Request1.RequestID, TrackIT.dbo.Sending.Barcode, TrackIT.dbo.Address_View.AdrID, TrackIT.dbo.Address_View.Name,
TrackIT.dbo.Address_View.Street, TrackIT.dbo.Address_View.HouseNo, TrackIT.dbo.Address_View.Postal, TrackIT.dbo.Address_View.City,
TrackIT.dbo.Address_View.Country, Request1.Latitude, Request1.Longitude, Request1.ReceivedDate
FROM (SELECT DISTINCT RequestID, LTRIM([Content]) AS Barcode, Latitude, Longitude, ReceivedDate
FROM dbo.RequestWithLocation
WHERE (Site LIKE #Site) AND ([Content] <> '') AND (AddressID = '0') AND (ReceivedDate > DATEADD(day, -#ReceivedDate, GETDATE()))) AS Request1 INNER JOIN
TrackIT.dbo.Sending ON Request1.Barcode = TrackIT.dbo.Sending.Barcode INNER JOIN
TrackIT.dbo.Address_View ON TrackIT.dbo.Sending.DeliveryAdrID = TrackIT.dbo.Address_View.AdrID
ORDER BY TrackIT.dbo.Address_View.AdrID
RETURN
END
As you can see I have 2 parameters and I'm returning a table with the information. But I need to use linq for executing the this function.
Can anyone help? thanks

I am assuming that you are using a dbml (linq to sql) file.
Drag drop your User defined function from the server explorer in visual studio into your dbml.
Then from the datacontext object of your dbml you can call the function directly.
For example if your dbml file name is xyz.dbml then your datacontext object's name would be 'XyzDataContext', unless you have changed it.
Then try this.
XyzDataContext db = new XyzDataContext();
List<Fn_GetNoLocationAddressResult> = db.Fn_GetNoLocationAddress("site", 25).ToList();

Related

Complex types show no result for a stored procedure

I have a stored procedure I would like to use its result in a Web API (C#).
I must miss something since I'm getting no result nor in Complex Types (in EF model) neither in Functions Imports (I can see the stored procedure in Functions Imports but it does not return any value, as expected).
This is my stored procedure (I have erased some non-important data in order to make it shorter)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
#main_id BIGINT,
#id_ze_mish BIGINT,
#id_nof BIGINT,
#loggedInUser VARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION [TransactionUniteSingles]
OPEN SYMMETRIC KEY io_key DECRYPTION BY CERTIFICATE chn_cert
-- Step 1
UPDATE [dbo].[t1]
SET deleted = 1,
WHERE id_nof = #id_nof
AND id_ze = #id_ze_mish
-- Step 2
UPDATE [dbo].[t_no_nir]
SET update_stamp = GETDATE(),
[user_name] = #loggedInUser
WHERE id_nof = #id_nof
AND ms_zehut_mazmin = #id_ze_mish
-- Step 3
CREATE TABLE #mainPrice
(
id INT,
fName VARCHAR(20),
lName VARCHAR(20)
)
INSERT INTO #mainPrice
EXEC [dbo].[io_sp_get_some_data_foo] #id
IF(EXISTS(select * from #mainPrice))
BEGIN
DECLARE #totalAmount INT;
SELECT #totalAmount = (main_price + price_tip + price_visa)
FROM #mainPrice
DROP TABLE #mainPrice
UPDATE [dbo].[t_4]
SET amount = #totalAmount,
update_stamp = GETDATE(),
[user_name] = #loggedInUser
WHERE id_nof = #id_nof
AND id = #main_id
CLOSE ALL SYMMETRIC KEYS
COMMIT TRANSACTION [TransactionUniteSingles]
SELECT CAST(1 as BIT) as 'Status', 'Succeeded' as 'ReturnMessage'
END
ELSE
BEGIN
SELECT CAST(0 as BIT) as 'Status', 'ADMIN - Unite Singles as 'ReturnMessage'
END
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION [TransactionUniteSingles]
SELECT CAST(0 as BIT) as 'Status', 'ADMIN - Unite Singles' as 'ReturnMessage'
END CATCH
END
Please note - when running the stored procedure as standalone, it works perfectly and return what expected.
Check what happens when you call your procedure with NULL input parameters. If the result is not a table then EF will not generate the complex result.
Try following:
-- declare table at SP begin
DECLARE #ResultTable TABLE(
Status BIT NOT NULL,
ResultMessage NVARCHAR(50) NOT NULL
)
-- insert data where you need
INSERT INTO #ResultTable
(
Status,
ResultMessage
)
VALUES
( NULL, -- Status - bit
N'' -- ResultMessage - nvarchar(50)
)
-- at SP end select result from table
SELECT * FROM #ResultTable
I found out that my problem is with my temp table.
when using SET FMTONLY OFF at the top of my SP or above the temp table(#mainPrice in my case) then everything works like a charm and suddenly the Complex Types shows my SP.
Another solution is using a variable table: DECLARE TABLE #mainPrice.
Both do the work.
I'll be happy to know why it happens and why C# refused to get a temp table as part of my SP , but at the moment I found the solution needed.

Procedure not working because of unresolved reference to object

I'm writing a WPF application where at some point I'm trying to add new row to my database through procedure like below:
CREATE PROCEDURE dbo.InsertStudent
#IdStudent INT,
#FirstName VARCHAR(50),
#LastName VARCHAR(50),
#Address VARCHAR(50),
#IndexNumber VARCHAR(50),
#IdStudies INT
AS
SET NOCOUNT ON
INSERT INTO [dbo].[apbd.Student]
([IdStudent]
,[FirstName]
,[LastName]
,[Address]
,[IndexNumber]
,[IdStudies])
VALUES
(#IdStudent
,#FirstName
,#LastName
,#Address
,#IndexNumber
,#IdStudies)
but whenever I'm about to use it, I'm getting error:
SQL71502: Procedure: [dbo].[InsertStudent] has an unresolved reference to object [dbo].[apbd.Student].
I was looking for solution but what I've found was only to add reference to database through right click on References and so on, but I do not have this option in my solution explorer.
Maybe I'm looking for it in wrong places but the only options I have after right click are something like this:
Add reference...
Add reference to service...
Add connected/concatenated/accumulative (or however should it be translated) service
Add analyzer...
Manage NuGet packets...
as for the code behind creation of the tables in database:
CREATE SCHEMA apbd;
GO
-- tables
-- Table: Student
CREATE TABLE apbd.Student (
IdStudent int NOT NULL IDENTITY,
FirstName nvarchar(100) NOT NULL,
LastName nvarchar(100) NOT NULL,
Address nvarchar(100) NOT NULL,
IndexNumber nvarchar(50) NOT NULL,
IdStudies int NOT NULL,
CONSTRAINT Student_pk PRIMARY KEY (IdStudent)
);
-- Table: Student_Subject
CREATE TABLE apbd.Student_Subject (
IdStudentSubject int NOT NULL IDENTITY,
IdStudent int NOT NULL,
IdSubject int NOT NULL,
CreatedAt datetime NOT NULL,
CONSTRAINT Student_Subject_pk PRIMARY KEY (IdStudentSubject,IdStudent,IdSubject)
);
-- Table: Studies
CREATE TABLE apbd.Studies (
IdStudies int NOT NULL IDENTITY,
Name nvarchar(100) NOT NULL,
CONSTRAINT Studies_pk PRIMARY KEY (IdStudies)
);
-- Table: Subject
CREATE TABLE apbd.Subject (
IdSubject int NOT NULL IDENTITY,
Name nvarchar(100) NOT NULL,
CONSTRAINT Subject_pk PRIMARY KEY (IdSubject)
);
-- End of file.
A MS SQL Server database, by default, only has a single schema (dbo). You can add schemas to group things for either security or organizational purposes.
In your case, the schema apbd was created and Student was created on that schema not the dbo schema. So, to reference that table, you need to use [apbd].[Student].
I would run the following to determine the actual name and schema of the table:
SELECT
CAST(
MAX(
CASE
WHEN
TABLE_SCHEMA = 'apbd'
AND TABLE_NAME = 'Student'
THEN 1
ELSE 0
END
) AS bit
) [The table is apbd.Student]
,
CAST(
MAX(
CASE
WHEN
TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'apbd.Student'
THEN 1
ELSE 0
END
) AS bit
) [The table is dbo.apbd.Student]
FROM INFORMATION_SCHEMA.TABLES
I'm also wondering if you perhaps need a USE statement at the start of your CREATE script - are you creating the procedure on the right database?
If the table is on a different database you would need to reference the database in your stored procedure, i.e. [DatabaseName].[dbo].[apbd.Student].

How to insert the foreign key into a subtype table in SQL Server

I have a supertype table called Student, and its subtype table called OtherStudents. I am wondering how to write a stored procedure that will insert the foreign key value into OtherStudents table every time a new record with a student type Other is inserted into Student.
Student table:
CREATE TABLE [dbo].[Student]
(
[STUD_ID] [INT] IDENTITY(1000009,1) NOT NULL,
[STUD_NAM] [VARCHAR](30) NOT NULL,
[STUD_EMAIL] [VARCHAR](50) NULL,
[CAMP_NAM] [VARCHAR](50) NULL,
[CAMP_ZIP] [INT] NULL,
[STUD_TYP] [CHAR](5) NOT NULL,
CONSTRAINT [PK_Student]
PRIMARY KEY CLUSTERED ([STUD_ID] ASC)
)
GO
ALTER TABLE [dbo].[Student] WITH CHECK
ADD CONSTRAINT [FK_Student_Campus]
FOREIGN KEY([CAMP_NAM], [CAMP_ZIP]) REFERENCES [dbo].[Campus] ([CAMP_NAM], [CAMP_ZIP])
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_Campus]
GO
`OtherStudents` table:
CREATE TABLE [dbo].[OtherStudents]
(
[OSTUD_ID] [INT] IDENTITY(1000009,1) NOT NULL,
[STUD_ST] [VARCHAR](30) NULL,
[STUD_APT] [VARCHAR](5) NULL,
[STUD_CITY] [CHAR](6) NULL,
[STUD_STATE] [CHAR](2) NULL,
[STUD_ZIP] [INT] NULL,
[RPT_ATTM] [VARBINARY](4000) NULL,
[RPT_EYESCR] [VARBINARY](4000) NULL,
[DATE_LASTPASS] [DATE] NULL,
[DATE_LASTVSP] [DATE] NULL,
[STUD_ID] [INT] NULL,
CONSTRAINT [PK_OtherStudents]
PRIMARY KEY CLUSTERED ([OSTUD_ID] ASC)
)
GO
ALTER TABLE [dbo].[OtherStudents] WITH CHECK
ADD CONSTRAINT [FK_OtherStudents_Student]
FOREIGN KEY([STUD_ID]) REFERENCES [dbo].[Student] ([STUD_ID])
GO
ALTER TABLE [dbo].[OtherStudents] CHECK CONSTRAINT [FK_OtherStudents_Student]
GO
I wrote two stored procedures:
ALTER PROCEDURE [dbo].[BusPassStudents_Insert]
(#STUD_ST VARCHAR(30),
#STUD_APT VARCHAR(5),
#STUD_CITY CHAR(6),
#STUD_STATE CHAR(2),
#STUD_ZIP INT,
#RPT_ATTM AS VARBINARY(4000) = NULL,
#RPT_EYESCR AS VARBINARY(4000) = NULL,
#DATE_LASTPASS DATE,
#DATE_LASTVSP AS DATE = NULL)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.OtherStudents (STUD_ST, STUD_CITY, STUD_STATE, STUD_ZIP, RPT_ATTM, RPT_EYESCR,DATE_LASTPASS, DATE_LASTVSP)
VALUES (#STUD_ST, #STUD_CITY, #STUD_STATE, #STUD_ZIP, #RPT_ATTM, #RPT_EYESCR, #DATE_LASTPASS, #DATE_LASTVSP)
END
ALTER PROCEDURE [dbo].[Stud_InsertNew]
(#STUD_NAM VARCHAR(30),
#STUD_EMAIL VARCHAR(50),
#CAMP_NAM VARCHAR(50),
#CAMP_ZIP INT,
#STUD_TYP CHAR(5))
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Student(STUD_NAM, STUD_EMAIL, CAMP_NAM, CAMP_ZIP, STUD_TYP)
VALUES (#STUD_NAM, #STUD_EMAIL, #CAMP_NAM, #CAMP_ZIP, #STUD_TYP);
DECLARE #STUD_ID INT
SET #STUD_ID = SCOPE_IDENTITY()
SELECT #STUD_ID
WHERE #STUD_TYP = 'Other'
END
Here is my C# code I have:
int campzip = int.Parse(ddlCamp.SelectedValue.Trim());
int StudentZip = int.Parse(txtStdZip.Text);
DateTime DateLastPass = DateTime.ParseExact(txtDateLastPass.Text, "yyyy-MM-dd", null);
using (var connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("cis-laredoConnectionString")))
{
connection.Open();
var cmd = new SqlCommand("dbo.Stud_InsertNew", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#STUD_NAM", txtStdName.Text);
cmd.Parameters.AddWithValue("#STUD_EMAIL", txtStdEmail.Text);
cmd.Parameters.AddWithValue("#CAMP_NAM", ddlCamp.SelectedItem.ToString());
cmd.Parameters.AddWithValue("#CAMP_ZIP", campzip);
cmd.Parameters.AddWithValue("#STUD_TYP", "Other");
int getStudID = (int) cmd.ExecuteScalar();
cmd.Dispose();
connection.Close();
connection.Open();
var cmd2 = new SqlCommand("BusPassStudents_Insert", connection);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("#STUD_ID", getStudID);
cmd2.Parameters.AddWithValue("#STUD_ST", txtStdStreet.Text);
cmd2.Parameters.AddWithValue("#STUD_APT", txtStdApt.Text);
cmd2.Parameters.AddWithValue("#STUD_CITY", txtStdCity.Text);
cmd2.Parameters.AddWithValue("#STUD_STATE", txtStdState.Text);
cmd2.Parameters.AddWithValue("#STUD_ZIP", StudentZip);
cmd2.Parameters.AddWithValue("#RPT_ATTM", fuAttend.FileBytes);
cmd2.Parameters.Add("#DATE_LASTPASS", SqlDbType.Date).Value = DateLastPass;
cmd2.ExecuteNonQuery();
cmd2.Dispose();
connection.Close();
}
I am trying to use ExecuteScalar to retrieve the inserted STUD_ID in Student table and post it back into the OtherStudents table.
I am getting error:
System.InvalidCastException: 'Specified cast is not valid.'
Any help would be appreciated!
You have to select something from your stored procedure to get the scalar value in the C# which is you missed, so in your Stud_InsertNew you can select the STUD_ID and use ExecuteScalar() in C# code as:
int getStudID = (int)cmd.ExecuteScalar();
Changes in stored procedure:
ALTER PROCEDURE [dbo].[Stud_InsertNew]
(#STUD_NAM VARCHAR(30),
#STUD_EMAIL VARCHAR(50),
#CAMP_NAM VARCHAR(50),
#CAMP_ZIP INT,
#STUD_TYP CHAR(5))
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Student(STUD_NAM, STUD_EMAIL, CAMP_NAM, CAMP_ZIP, STUD_TYP)
VALUES (#STUD_NAM, #STUD_EMAIL, #CAMP_NAM, #CAMP_ZIP, #STUD_TYP);
// Need this line which returns the latest inserted Student Id
SELECT TOP 1 [STUD_ID]
FROM dbo.Student
ORDER BY [STUD_ID] DESC
END
After a comment from Mitch Wheat and also found here, that the best way to use SCOPE_IDENTIY() if you want to get the latest inserted records primary key's value, so you just need to change the SELECT statement to this:
DECLARE #STUD_ID INT
SET #STUD_ID = SCOPE_IDENTITY()
SELECT #STUD_ID
This is how I'd like to approach this.
Convert two procedures into a single one by passing all the required parameters.
Make use of the "inserted" table data to get the information that was just Inserted into table A and store the reference in table B.
*(i) You may want to pass the rest of the parameters required to store the data in table B. I haven't included them in the stored procedure.
(ii) I'm trying to insert data into table B if only the check on birthday passes. In your case the predicate/condition will differ.
USE SOMEDB;
CREATE TABLE A
(Id Integer IDENTITY(1,1),
[Name] VARCHAR(20),
[Birthday] DATE
)
create table B
(
B_Id INTEGER IDENTITY(10001,1),
A_Id Integer NULL,
ColumnX VARCHAR(20),
ColumnY varchar(20))
CREATE PROCEDURE [uspInsertIndividualDetails]
(#name varchar(20),
#birthday date
)
AS
BEGIN
CREATE TABLE #tmpXYZ(iD INT,birthday date);
INSERT INTO A([Name],[Birthday])
OUTPUT inserted.Id,inserted.Birthday into #tmpXYZ
select #name, #birthday
IF exists (select 1 from #tmpXYZ where Birthday >'2018-01-01')
begin
INSERT INTO B(A_Id,ColumnX, ColumnY)
SELECT Id,'A Value', 'Some Other Value' FROM #tmpXYZ
end
END
execute [uspInsertIndividualDetails] #name ='John Doe', #birthday = '1972-10-01'
SELECT * FROM a
SELECT * FROM b
execute [uspInsertIndividualDetails] #name ='John DoeJr', #birthday = '2018-10-01'
SELECT * FROM a
SELECT * FROM b
Hope this helps.
You can do this without making database connection twice by making one stored procedure which combines the two stored procedure's input parameters as:
Reformed stored procedure:
ALTER PROCEDURE [dbo].[Stud_InsertNew]
(#STUD_NAM VARCHAR(30),
#STUD_EMAIL VARCHAR(50),
#CAMP_NAM VARCHAR(50),
#CAMP_ZIP INT,
#STUD_TYP CHAR(5),
// Parameters for other student
#STUD_APT VARCHAR(5),
#STUD_CITY CHAR(6),
#STUD_STATE CHAR(2),
#STUD_ZIP INT,
#RPT_ATTM AS VARBINARY(4000) = NULL,
#RPT_EYESCR AS VARBINARY(4000) = NULL,
#DATE_LASTPASS DATE,
#DATE_LASTVSP AS DATE = NULL)
AS
BEGIN
DECLARE #OTHERCONSTANT NVARCHAR(MAX)
SET #OTHERCONSTANT = 'Other' // You can set to a text that coming from C#
SET NOCOUNT ON
INSERT INTO dbo.Student(STUD_NAM, STUD_EMAIL, CAMP_NAM, CAMP_ZIP, STUD_TYP)
VALUES (#STUD_NAM, #STUD_EMAIL, #CAMP_NAM, #CAMP_ZIP, #STUD_TYP);
IF(#STUD_TYP = #OTHERCONSTANT)
BEGIN
// Need this line which returns the latest inserted Student Id
DECLARE #STUD_ID INT = NULL
SET #STUD_ID = SCOPE_IDENTITY()
INSERT INTO dbo.OtherStudents (STUD_ST, STUD_CITY, STUD_STATE, STUD_ZIP, RPT_ATTM, RPT_EYESCR,DATE_LASTPASS, DATE_LASTVSP)
VALUES (#STUD_ST, #STUD_CITY, #STUD_STATE, #STUD_ZIP, #RPT_ATTM, #RPT_EYESCR, #DATE_LASTPASS, #DATE_LASTVSP)
END
END
C# code:
using(var connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("cis-laredoConnectionString"))) {
connection.Open();
var cmd = new SqlCommand("dbo.Stud_InsertNew", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#STUD_NAM", txtStdName.Text);
cmd.Parameters.AddWithValue("#STUD_EMAIL", txtStdEmail.Text);
cmd.Parameters.AddWithValue("#CAMP_NAM", ddlCamp.SelectedItem.ToString());
cmd.Parameters.AddWithValue("#CAMP_ZIP", campzip);
cmd.Parameters.AddWithValue("#STUD_TYP", "Other");
cmd.Parameters.AddWithValue("#STUD_ST", txtStdStreet.Text);
cmd.Parameters.AddWithValue("#STUD_APT", txtStdApt.Text);
cmd.Parameters.AddWithValue("#STUD_CITY", txtStdCity.Text);
cmd.Parameters.AddWithValue("#STUD_STATE", txtStdState.Text);
cmd.Parameters.AddWithValue("#STUD_ZIP", StudentZip);
cmd.Parameters.AddWithValue("#RPT_ATTM", fuAttend.FileBytes);
cmd.Parameters.Add("#DATE_LASTPASS", SqlDbType.Date).Value = DateLastPass;
cmd.ExecuteNonQuery();
cmd.Dispose();
if (connection.State == ConnectionState.Open) {
connection.Close();
}
}

Error Must declare the scalar variable with Table Valued Parameter

I am not getting what I am doing wrong here. From the update segment, I am getting this error:
Must declare the scalar variable "#OrderTestTVP".
Here is my table-valued parameter type
CREATE TYPE [dbo].[TVP_OrderTest] AS TABLE(
[OrderTestId] [int] NULL,
[OrderId] [int] NOT NULL,
[TestCode] [varchar](10) NOT NULL
)
GO
ALTER PROCEDURE [dbo].[TVP_Save_OrderTest]
#OrderTestTVP TVP_OrderTest READONLY
AS
BEGIN
BEGIN TRY
DECLARE #TableOfIdentities TABLE (IdentValue BIGINT, TestCode varchar(10) )
INSERT INTO OrderTest
(
OrderId
,TestCode
)
OUTPUT Inserted.OrderTestId,inserted.TestCode
INTO #TableOfIdentities(IdentValue,TestCode)
SELECT OrderId
,TestCode
FROM #OrderTestTVP
WHERE OrdertestID = 0
UPDATE
OrderTest
SET
OrderId = #OrderTestTVP.OrderId,
TestCode = #OrderTestTVP.TestCode
FROM OrderTest INNER JOIN #OrderTestTVP
ON OrderTest.OrderTestId = #OrderTestTVP.OrderTestId
SELECT * FROM #TableOfIdentities
END TRY
BEGIN CATCH
exec error_catch
END CATCH
END
I'd say you have to use a table alias for the join operation:
UPDATE
OrderTest
SET
OrderId = o.OrderId,
TestCode = o.TestCode
FROM OrderTest INNER JOIN #OrderTestTVP o
ON OrderTest.OrderTestId = o.OrderTestId
I have no SQL at hand right now to try but if I recall correctly, table variables need aliases if used in expressions.

Passing C# parameters to a stored procedure report

I am trying to trigger a report to run with "select all" checked for a field FiscalYearId, which is a string type. I can successfully pass one string such as "2" and the report will find it. However, when I try to pass "select all" or "2,3,4", the multi-check field does not pick up on this.
Here is part of the stored procedure from the report:
ALTER PROCEDURE [dbo].[...]
#FiscalYearId varchar (100) = null ,
...
select ...
where ...
and (#FiscalYearID IS NULL OR p.FiscalYearId IN (SELECT * FROM SPLIT(#FiscalYearID, ',')))
my c# code which triggers the report to pop up in a radwindow is:
`string years = "2,3,4";
newWindow.NavigateUrl = "../Reporting/SingleReport.aspx?Report=AdHocSourcingReport&VendorID="
+ vendorId + "&VendorReport=true" + "&FiscalYearId="+years;`
Currently, the vendorId string and VendorReport boolean pull through successfully, it is the string separated by commas that is not populating when the report is run. I initially tried to send a "select all", which is what I want.. Any help is greatly appreciated!!
This is how I've handled splitting comma delimited strings in the past:
CREATE FUNCTION
[dbo].[GetItemTable]
(
#Items VARCHAR(1000)
)
RETURNS
#ItemTable TABLE
(
RowID INT IDENTITY(1,1) PRIMARY KEY,
item VARCHAR(30)
)
AS
BEGIN
DECLARE #ProcessItems VARCHAR(1000)
DECLARE #CurrentItem VARCHAR(30)
SET #ProcessItems = REPLACE(#Items, '''', '')
IF SUBSTRING(#ProcessItems, LEN(#ProcessItems), 1) ','
SET #ProcessItems = #ProcessItems + ','
WHILE CHARINDEX(',', #ProcessItems) > 0
BEGIN
SET #CurrentItem = LTRIM(CAST(SUBSTRING(#ProcessItems, 0, CHARINDEX(',', #ProcessItems)) AS VARCHAR(30)))
INSERT INTO
#ItemTable
(
item
)
VALUES
(
#CurrentItem
)
SET #ProcessItems = SUBSTRING(#ProcessItems, CHARINDEX(',', #ProcessItems) + 1, LEN(#ProcessItems))
END -- While Schedule
RETURN
END
Obviously, you can change the current item size to whatever you happen to need it to be.
Then just use it like this:
WHERE (p.FiscalYearId IN(SELECT item FROM dbo.GetItemTable(#FiscalYearID)))
I remember doing something similar couple years ago and having same kinds of problem with the list.
Can you try this...
ALTER PROCEDURE [dbo].[...] #FiscalYearId varchar (100) = null ,
... select... where... and
(#FiscalYearID IS NULL OR p.FiscalYearId IN (#FiscalYearID))
Also, if you are sending "Select all" string, you might need to have a condition in your procedure to check that and change the query to "Select id from fiscalyeartable"

Categories