BrianD
2012-08-24 16:22:28 UTC
I'd like to return a table from a function using two passed parameters. One parameter works for a start date. The other, representing a parameter for a field, returns a false result.
I believe the right question to ask is how to pass a parameter for a field.
The requirement for a project is to create a job that runs an SSIS routine to export the most recent updated records from a table. For security reasons, I have very limited access to the database, and will walk in on a scheduled date to install the package and create the job.
As the SSIS package must return records by passing in a start date, or start key parameter, from which records will be extracted, it seems a user-defined function is the best option.
I'm beginning with the start date scenario. Start ID hasn't been considered yet, but I'm attempting to leave that option open by using @UserField parameter. There would also be @UserTable as a parameter (because I don't yet know which tables to query). For now, I'm still in troubleshooting mode, so I'm reducing the possible sources for error.
The function below should work (I think), but doesn't. Instead, it returns all records.
When I remove the @UserField parameter, it does correctly return a subset of the records after the passed start date.
I'm stumped and can't get past this problem. I could really use a little assist. Can anyone suggest a solution to the @UserField parameter issue?
Create table procedure and records are below as well.
Any help is very much appreciated.
Cheers.
--#########################
--CREATE PARAMETER FUNCTION
--RETURNS TABLE
USE OPCDC;
GO
IF OBJECT_ID (N'fn_ExportByStartParameter', N'IF') IS NOT NULL
DROP FUNCTION fn_ExportByStartParameter;
GO
CREATE FUNCTION fn_ExportByStartParameter (@UserField CHAR(30), @UserValue CHAR(10))
RETURNS TABLE
AS
RETURN
(
SELECT *
FROM
OPCDC.dbo.Dummy
WHERE @UserField >= @UserValue
);
GO
--#########################
--CREATE DUMMY TABLE
USE [OPCDC]
GO
/****** Object: Table [dbo].[Dummy] Script Date: 08/24/2012 11:14:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Dummy](
[DummyPK] [int] IDENTITY(1,1) NOT NULL,
[NumberInteger] [int] NULL,
[Description] [nvarchar](20) NULL,
[DateStamp] [date] NULL,
[DateStamp2] [date] NULL
) ON [PRIMARY]
GO
DummyPK NumberInteger Description DateStamp DateStamp2
1 1 One 2012-01-01 2012-08-01
2 2 Two 2012-02-01 2012-08-02
3 3 Three 2012-03-01 2012-08-03
4 4 Four 2012-04-01 2012-08-04
5 5 Five 2012-05-01 2012-08-05
6 6 Six 2012-06-01 2012-08-06
7 7 Seven 2012-07-01 2012-08-07
8 8 Eight 2012-08-01 2012-08-08
9 9 Nine 2012-09-01 2012-08-09
10 10 Ten 2012-10-01 2012-08-10
11 11 Eleven 2012-11-01 2012-08-11
12 12 Twelve 2012-12-01 2012-08-12
I believe the right question to ask is how to pass a parameter for a field.
The requirement for a project is to create a job that runs an SSIS routine to export the most recent updated records from a table. For security reasons, I have very limited access to the database, and will walk in on a scheduled date to install the package and create the job.
As the SSIS package must return records by passing in a start date, or start key parameter, from which records will be extracted, it seems a user-defined function is the best option.
I'm beginning with the start date scenario. Start ID hasn't been considered yet, but I'm attempting to leave that option open by using @UserField parameter. There would also be @UserTable as a parameter (because I don't yet know which tables to query). For now, I'm still in troubleshooting mode, so I'm reducing the possible sources for error.
The function below should work (I think), but doesn't. Instead, it returns all records.
When I remove the @UserField parameter, it does correctly return a subset of the records after the passed start date.
I'm stumped and can't get past this problem. I could really use a little assist. Can anyone suggest a solution to the @UserField parameter issue?
Create table procedure and records are below as well.
Any help is very much appreciated.
Cheers.
--#########################
--CREATE PARAMETER FUNCTION
--RETURNS TABLE
USE OPCDC;
GO
IF OBJECT_ID (N'fn_ExportByStartParameter', N'IF') IS NOT NULL
DROP FUNCTION fn_ExportByStartParameter;
GO
CREATE FUNCTION fn_ExportByStartParameter (@UserField CHAR(30), @UserValue CHAR(10))
RETURNS TABLE
AS
RETURN
(
SELECT *
FROM
OPCDC.dbo.Dummy
WHERE @UserField >= @UserValue
);
GO
--#########################
--CREATE DUMMY TABLE
USE [OPCDC]
GO
/****** Object: Table [dbo].[Dummy] Script Date: 08/24/2012 11:14:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Dummy](
[DummyPK] [int] IDENTITY(1,1) NOT NULL,
[NumberInteger] [int] NULL,
[Description] [nvarchar](20) NULL,
[DateStamp] [date] NULL,
[DateStamp2] [date] NULL
) ON [PRIMARY]
GO
DummyPK NumberInteger Description DateStamp DateStamp2
1 1 One 2012-01-01 2012-08-01
2 2 Two 2012-02-01 2012-08-02
3 3 Three 2012-03-01 2012-08-03
4 4 Four 2012-04-01 2012-08-04
5 5 Five 2012-05-01 2012-08-05
6 6 Six 2012-06-01 2012-08-06
7 7 Seven 2012-07-01 2012-08-07
8 8 Eight 2012-08-01 2012-08-08
9 9 Nine 2012-09-01 2012-08-09
10 10 Ten 2012-10-01 2012-08-10
11 11 Eleven 2012-11-01 2012-08-11
12 12 Twelve 2012-12-01 2012-08-12