I am trying to create a stored procedure but am getting these errors:
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 42
Invalid column name 'Date_Collected'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 43
Invalid column name 'Time_Collected'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 43
Invalid column name 'Date_Entered'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 44
Invalid column name 'Time_Entered'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 45
Invalid column name 'Date_Completed'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 46
Invalid column name 'Time_Completed'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 47
Invalid column name 'Test_Date'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 48
Invalid column name 'Test_Time'.
here is the full source:
USE [SalesDWH]
GO
/****** Object: StoredProcedure [dbo].[Insert_QuickLabDump] Script Date: 12/22/2011 14:52:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER on
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
create PROCEDURE [dbo].[Insert_QuickLabDump]
-- Add the parameters for the stored procedure here
#Specimen_ID [varchar](50),
#Client_Key int,
#Outcome [varchar](50),
#Medications [varchar] (max),
#Date_Collected date,
#Time_Collected time ,
#Date_Entered date,
#Time_Entered time ,
#Date_Completed date,
#Time_Completed time ,
#Test_Date date ,
#Test_Time time ,
#Practice_Name [varchar] (500),
#Practice_Code [varchar] (500),
#Client_ID [varchar] (500),
#Requesting_Physician [varchar] (500),
#Other_Medications [varchar] (max),
#Order_Comments [varchar] (max),
#Reference_Number [varchar] (500),
#Order_Count int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
INSERT INTO [SalesDWH].[dbo].[QuickLabDump]
([Specimen ID]
,[Client Key]
,[Outcome]
,[Medications]
,[Date_Collected]
,[Time_Collected]
,[Date_Entered]
, [Time_Entered]
, Date_Completed
, Time_Completed
, Test_Date
, Test_Time
,[Practice Name]
,[Practice Code]
,[Client ID]
,[Requesting Physician]
,[Other Medications]
,[Order Comments]
,[Reference Number]
,[Order Count]
)
VALUES
(#Specimen_ID,
#Client_Key,
#Outcome,
#Medications,
#Date_Collected ,
#Time_Collected ,
#Date_Entered,
#Time_Entered ,
#Date_Completed ,
#Time_Completed,
#Test_Date ,
#Test_Time,
#Practice_Name,
#Practice_Code,
#Client_ID,
#Requesting_Physician,
#Other_Medications,
#Order_Comments,
#Reference_Number,
#Order_Count
)
END
What am I doing wrong?
here is the table structure:
USE [SalesDWH]
GO
/****** Object: Table [dbo].[QuickLabDump] Script Date: 12/22/2011 15:13:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[QuickLabDump](
[id] [int] IDENTITY(1,1) NOT NULL,
[Specimen ID] [varchar](50) NOT NULL,
[Client Key] [int] NOT NULL,
[Outcome] [varchar](50) NOT NULL,
[Medications] [varchar](max) NULL,
[Date Collected] [date] NOT NULL,
[Time Collected] [time](7) NOT NULL,
[Date Entered] [date] NOT NULL,
[Time Entered] [time](7) NOT NULL,
[Date Completed] [date] NOT NULL,
[Time Completed] [time](7) NOT NULL,
[Test Date] [date] NOT NULL,
[Test Time] [time](7) NOT NULL,
[Practice Name] [varchar](500) NOT NULL,
[Practice Code] [varchar](500) NOT NULL,
[Client ID] [varchar](500) NULL,
[Requesting Physician] [varchar](500) NULL,
[Other Medications] [varchar](max) NULL,
[Order Comments] [varchar](max) NULL,
[Reference Number] [varchar](500) NULL,
[Order Count] [int] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
The errors are saying that there are no such columns (for example Date_Collected) in the QuickLabDump table. For insert statements, you have to have the right column names. You have a column named Date Collected, not Date_Collected, for example (note the space instead of the underscore).
It looks like you added those columns to an existing procedure. Have you added the columns to the database table you are trying to insert to?
You are using [Date_Collected] in your stored procedure, but the actual column name according to the CREATE TABLE statement is [Date Collected] (no underscore).
[Date Collected] [date] NOT NULL,
is not the same as Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 42
Invalid column name 'Date_Collected'.
Related
I am trying to write a stored procedure that creates a new row of data in my table that has an Identity column. I can't figure out what I'm doing wrong to get it to store my data in the table.
Here is my table
CREATE TABLE [Promotional].[Proposals]
(
[Proposal_Uid] [int] IDENTITY(1,1) NOT NULL,
[Prime_Contract] [nvarchar](30) NULL,
[Sub_Contract] [nvarchar](30) NULL,
[Po_Id] [nvarchar](30) NULL,
[Proposal_Title] [nvarchar](50) NULL,
[Client_Name] [nvarchar](40) NULL,
[Client_Code] [nvarchar](20) NULL,
[Total_Proposal_Amount] [decimal](18, 2) NULL,
[ODC_Amount] [decimal](18, 2) NULL,
[Manager_Name] [nvarchar](30) NULL,
[Admin] [nvarchar](30) NULL,
[Due_Date] [date] NULL,
[Start_Date] [date] NULL,
[End_Date] [date] NULL,
[Proposal_Number] [nvarchar](20) NULL,
[Contract_Type] [nvarchar](16) NULL,
CONSTRAINT [Proposal_Uid]
PRIMARY KEY CLUSTERED ([Proposal_Uid] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Here is my stored procedure:
ALTER PROCEDURE [Promotional].[Proposals_Create]
(#Prime_Contract nvarchar(30),
#Sub_Contract nvarchar(30),
#Po_Id nvarchar(30),
#Proposal_Title nvarchar(50),
#Client_Name nvarchar(40),
#Client_Code nvarchar(20),
#Total_Proposal_Amount decimal(18,2),
#ODC_Amount decimal(18,2),
#Manager_Name nvarchar(30),
#Admin nvarchar(30),
#Due_Date date,
#Start_Date date,
#End_Date date,
#Proposal_Number nvarchar(20),
#Contract_Type nvarchar(16),
#NewId int output)
AS
BEGIN
IF #NewId IS NULL
BEGIN
INSERT INTO Promotional.Proposals (Prime_Contract, Sub_Contract, Po_Id, Proposal_Title, Client_Name, Client_Code,
Total_Proposal_Amount, ODC_Amount, Manager_Name, Admin, Due_Date, Start_Date, End_Date, Proposal_Number, Contract_Type)
VALUES (#Prime_Contract, #Sub_Contract, #Po_Id, #Proposal_Number, #Client_Name, #Client_Code,
#Total_Proposal_Amount, #ODC_Amount, #Manager_Name, #Admin, #Due_Date, #Start_Date, #End_Date,
#Proposal_Number, #Contract_Type)
SET #NewId = SCOPE_IDENTITY()
END
RETURN #NewId
END
GO
I know it's something small I missed. But, my OUTCOME is that when you run the procedure, it inserts the values in the correct columns, and the primary key is auto updated without having to stick your own value into the statement. My execution of the statement would be as follows
EXEC Promotional.Proposals_Create 'hj','fd','fd','fd','fd','fd','0.23','1.24','fd','fd','2020/08/30','2020/08/30','2020/08/30','fd','fd';
And I get this error:
Msg 201, Level 16, State 4, Procedure Promotional.Proposals_Create, Line 0 [Batch Start Line 0]
Procedure or function 'Proposals_Create' expects parameter '#NewId', which was not supplied.
I shouldn't have to provide that parameter because it should auto update with my PK which is Proposal_Uid. Or do I have to make my Proposal_Uid the value returned from ScopeIdentity?
#NewID should not be a parameter of the procedure since it is an IDENTITY column.
You can get the stored procedure to return the generated id by providing an output variable to store its content. This behaviour is also documented here.
declare #test int;
EXEC Proposals_Create 'hj','fd','fd','fd','fd','fd','0.23','1.24','fd','fd',
'2020/08/30','2020/08/30','2020/08/30','fd','fd', #test OUTPUT;
select #test as 'new_identity';
set #test = null; -- because of the IF statement in your procedure...
EXEC Proposals_Create 'hj','fd','fd','fd','fd','fd','0.23','1.24','fd','fd',
'2020/08/30','2020/08/30','2020/08/30','fd','fd', #test OUTPUT;
select #test as 'new_identity';
The selects return 1 and 2 respectively.
Fiddle (Remark: schema name removed in code above and in fiddle).
I'm trying to do some testing on some tables but I seem to keep running into an error.
I'm trying to run the following stored procedure:
INSERT INTO [Customer].[TableA_test] ([CustomerID], [VisitID], [TransactionId], [ArrivalDT], [DataA], [DataB], [DataC], [SnapDate])
SELECT
b.CustomerID,
b.VisitID,
b.TransactionID,
b.ArrivalDT,
b.DataA,
b.DataB,
b.DataC,
SnapDate = GETDATE()
FROM
[Customer].[TableA_test] a
LEFT JOIN
[Customer].[TableB] b ON a.VisitID = b.VisitID
AND a.TransactionId = b.TransactionID
WHERE
a.TransactionID IS NULL
The procedure is meant to take the results from the query and insert them into the TableA. Table A has the exact same columns and data types as TableB. When I try to execute the stored procedure (code provided above), I get the following error:
Msg 241, Level 16, State 1, Procedure InsertTo_TableA_test, Line 27
Conversion failed when converting date and/or time from character string.
All of my date columns are set to datetime data type: ArrivalDT and the SnapDate. TableA_testing and TableB have the exact same column names and datatypes.
Is there something I'm missing?
Here's the table info for [Customer].[TableA_test]:
CREATE TABLE [Customer].[TableA_test]
(
[TransactionID] [VARCHAR](254) NULL,
[VisitID] [VARCHAR](254) NULL,
[ArrivalDT] [DATETIME] NULL,
[CustomerID] [VARCHAR](254) NULL,
[DataA] [VARCHAR](50) NULL,
[DataB] [VARCHAR](50) NULL,
[DataC] [VARCHAR](50) NULL,
[SnapDate] [DATETIME] NULL
)
And for TableB:
CREATE TABLE [Customer].[TableB]
(
[TransactionID] [VARCHAR](254) NULL,
[VisitID] [VARCHAR](254) NULL,
[ArrivalDT] [DATETIME] NULL,
[CustomerID] [VARCHAR](254) NULL,
[DataA] [VARCHAR](50) NULL,
[DataB] [VARCHAR](50) NULL,
[DataC] [VARCHAR](50) NULL,
[SnapDate] [DATETIME] NULL
)
Line 27 of my stored procedure is:
INSERT INTO [Customer].[JTM_NY_SPARCS_VitalSigns_test] ([CustomerID],[VisitID], [TransactionID], [ArrivalDT], [DataA], [DataB], [DataC], [SnapDate])
Here's the full read-out of the stored procedure:
USE [Database_3]
GO
/****** Object: StoredProcedure [Customer].[InsertTo_TableA_test] Script Date: 4/12/2019 11:49:17 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Customer].[InsertTo_TableA_test]
/*
CREATED: 04/11/2019
*/
AS
BEGIN
INSERT INTO [Customer].[TableA_test] ([CustomerID], [VisitID], [TransactionId], [ArrivalDT], [DataA], [DataB], [DataC], [SnapDate])
SELECT
b.CustomerID,
b.VisitID,
b.TransactionID,
b.ArrivalDT,
b.DataA,
b.DataB,
b.DataC,
SnapDate = GETDATE()
FROM
[Customer].[TableA_test] a
LEFT JOIN
[Customer].[TableB] b ON a.VisitID = b.VisitID
AND a.TransactionId = b.TransactionID
WHERE
a.TransactionID IS NULL
END
GO
Example of a row of results when running the query from the stored procedure:
100010 [VisitID]
20000281542 [TransactionID]
2014-07-09 15:44:42.000 [ArrivalDT]
0032011 [CustomerID]
147 [DataA]
71 [DataB]
69 [DataC]
2019-04-12 11:54:23.753 [SnapDate]
From what I have seen in your comments, you said you are trying to insert from TableA_test to TableB. If that is the case, then your query should be:
INSERT INTO [Customer].[TableB] ([CustomerID],[VisitID],[TransactionId],[ArrivalDT],[DataA],[DataB],[DataC],[SnapDate])
SELECT
a.CustomerID,
a.VisitID,
a.TranactionID,
a.ArrivalDT,
a.DataA,
a.DataB,
a.DataC,
SnapDate = GETDATE()
FROM [Customer].[TableA_test] a
LEFT JOIN [Customer].[TableB] b
ON a.VisitID = b.VisitID
AND a.TransactionId = b.TransactionID
WHERE b.TransactionID IS NULL
Here is my table
CREATE TABLE [dbo].[RPost_Receipts]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[branch] [int] NULL,
[policyref] [varchar](10) NULL,
[receipt_email_received] [datetime] NULL,
[receipt_email_to_openattach] [int] NULL,
[receipt_email_to_openattach_dt] [datetime] NULL,
[sender_name] [varchar](50) NULL,
[sender_address] [varchar](100) NULL,
[subject] [varchar](160) NULL,
[message_id] [varchar](100) NULL,
[time_received] [datetime] NULL,
[delivery_to] [varchar](100) NULL,
[delivery_status] [varchar](100) NULL,
[delivery_report] [varchar](max) NULL,
[delivery_time_utc] [datetime] NULL,
[delivery_time_local] [datetime] NULL,
[time_opened] [datetime] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
This is the following UPDATE query I am attempting to run on the same table
UPDATE [FreshSystems].[dbo].[RPost_Receipts]
SET [sender_name] = 'RPost eSignOff Service' ,
[sender_address] = 'contracts#usw.rpost.net' ,
[subject] = 'Re: REF: 02-OGKX02PC01 Your Insurance Policy (2 of 2)' ,
[message_id] = '49B918875098C1EFCB5A33FDB2D446FF5C294ACE' ,
[time_received] = '9/15/2015 10:36:29 AM' ,
[delivery_to] = 'AutoSaintCS#Fresh.co.uk' ,
[delivery_status] = 'Delivered to Mailserver' ,
[delivery_report] = '250 OK id=1ZbnbS-0003fY-4y engine03-30179-2.icritical.com (192.162.216.4)' ,
[delivery_time_utc] = '9/15/2015 10:36:47 AM' ,
[delivery_time_local] = '9/15/2015 10:36:47 AM' ,
[time_opened] = 'NULL'
WHERE [branch] = 02
AND [policyref] = 'OGKX02PC01'
AND [delivery_to] IS NULL
Why am I receiving a conversion error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
The inserts are going into 'DATETIME' columns and the datetime's that are being inserted are all correctly formatted, can anyone shed any light on this for me please?
UPDATE
The problem seems to occur ONLY within my VB.NET Application. When running the statement in SQL Server Management Studio it is fine, when it executed within my VB.NET it fails as a conversation from character to Datetime.
Any suggestions?
the issue is you were trying to put the word NULL into the time_opened field. (which is a datetime field)
Change it from [time_opened] = 'NULL' to [time_opened] = NULL
UPDATE [FreshSystems].[dbo].[RPost_Receipts]
SET [sender_name] = 'RPost eSignOff Service' ,
[sender_address] = 'contracts#usw.rpost.net' ,
[subject] = 'Re: REF: 02-OGKX02PC01 Your Insurance Policy (2 of 2)' ,
[message_id] = '49B918875098C1EFCB5A33FDB2D446FF5C294ACE' ,
[time_received] = '9/15/2015 10:36:29 AM' ,
[delivery_to] = 'AutoSaintCS#Fresh.co.uk' ,
[delivery_status] = 'Delivered to Mailserver' ,
[delivery_report] = '250 OK id=1ZbnbS-0003fY-4y engine03-30179-2.icritical.com (192.162.216.4)' ,
[delivery_time_utc] = '9/15/2015 10:36:47 AM' ,
[delivery_time_local] = '9/15/2015 10:36:47 AM' ,
[time_opened] = NULL
WHERE [branch] = 02
AND [policyref] = 'OGKX02PC01'
AND [delivery_to] IS NULL
I think Datetime format is yy-mm-dd hh:mm:ss and not 9/15/2015 10:36:29 AM
Convert it before updating table.
I'm new to writing SQL Server triggers. I have a table called USERS and I also a another tabled called USERS_DELTA. The difference between the two is USERS_DELTA has one additional column called change_type.
Here are the table schema:
USERS table:
CREATE TABLE [dbo].[TDR_Users]
(
[objectGUID] [varbinary](50) NOT NULL,
[distinguishedName] [nvarchar](255) NOT NULL,
[adForest] [nvarchar](50) NULL,
[adDomain] [nvarchar](50) NULL,
[accountExpires] [datetime] NULL,
[adminCount] [int] NULL,
[cn] [nvarchar](64) NULL,
[company] [nvarchar](64) NULL,
[description] [nvarchar](448) NULL,
[displayName] [nvarchar](256) NULL,
[division] [nvarchar](256) NULL,
[employeeID] [nvarchar](16) NULL
)
And USERS_DELTA table:
CREATE TABLE [dbo].[TDR_Users]
(
[objectGUID] [varbinary](50) NOT NULL,
[distinguishedName] [nvarchar](255) NOT NULL,
[adForest] [nvarchar](50) NULL,
[adDomain] [nvarchar](50) NULL,
[accountExpires] [datetime] NULL,
[adminCount] [int] NULL,
[cn] [nvarchar](64) NULL,
[company] [nvarchar](64) NULL,
[description] [nvarchar](448) NULL,
[displayName] [nvarchar](256) NULL,
[division] [nvarchar](256) NULL,
[employeeID] [nvarchar](16) NULL,
[change_Type] [nvarchar](10) NULL
)
I have an application which will be creating records in USERS table. But what I'm trying to do is capture the inserts into the USERS_DELTA. I have written a trigger on the USERS table:
CREATE TRIGGER [dbo].[TR_INSERTS_DELTAS]
ON [dbo].[Users]
FOR INSERT
AS
DECLARE #ObjectGUID varbinary(50), #DN varchar(255), #memcount int;
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Get the primary and unique keys from the inserted rows.
SELECT #DN=i.distinguishedName FROM inserted i;
SELECT #ObjectGUID = i.objectGUID FROM inserted i;
-- Check if a row already exists in the TDR_Users_Delta table with those values.
SELECT #memcount=COUNT(*) FROM Users
WHERE Users.distinguishedName = #DN
AND Users.objectGUID = #ObjectGUID ;
if(#memcount = 0)
BEGIN
INSERT INTO [dbo].[Users_Delta]
(
[objectGUID],
[distinguishedName],
[adForest],
[adDomain],
[accountExpires],
[adminCount],
[cn] ,
[company],
[description],
[displayName],
[division],
[employeeID],
[change_type]
)
VALUES
(
INSERTED.[objectGUID],
INSERTED.[distinguishedName],
INSERTED.[adForest],
INSERTED.[adDomain],
INSERTED.[accountExpires],
INSERTED.[adminCount],
INSERTED.[cn] ,
INSERTED.[company],
INSERTED.[description],
INSERTED.[displayName],
INSERTED.[division],
INSERTED.[employeeID],
'Add'
);
END
END
GO
When I execute this trigger, I get the following error:
Msg 4104, Level 16, State 1, Procedure TR_INSERTS_DELTAS, Line 94
The multi-part identifier "Inserted.objectGUID" could not be bound.
Msg 4104, Level 16, State 1, Procedure TR_INSERTS_DELTAS, Line 95
The multi-part identifier "INSERTED.distinguishedName" could not be bound.
Msg 4104, Level 16, State 1, Procedure TR_INSERTS_DELTAS, Line 96
The multi-part identifier "INSERTED.adForest" could not be bound.
Msg 4104, Level 16, State 1, Procedure TR_INSERTS_DELTAS, Line 97
The multi-part identifier "INSERTED.adDomain" could not be bound.
Msg 4104, Level 16, State 1, Procedure TR_INSERTS_DELTAS, Line 98
...
What am I doing wrong? :(
I think you just need to put a select with the table in rather than using inserted.x to signify the insert.
CREATE TRIGGER [dbo].[TR_INSERTS_DELTAS]
ON [dbo].[Users]
FOR INSERT
AS
DECLARE #ObjectGUID varbinary(50), #DN varchar(255), #memcount int;
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Get the primary and unique keys from the inserted rows.
SELECT #DN=i.distinguishedName FROM inserted i;
SELECT #ObjectGUID = i.objectGUID FROM inserted i;
-- Check if a row already exists in the TDR_Users_Delta table with those values.
SELECT #memcount=COUNT(*) FROM Users
WHERE Users.distinguishedName = #DN
AND Users.objectGUID = #ObjectGUID ;
if(#memcount = 0)
BEGIN
INSERT INTO [dbo].[Users_Delta]
(
[objectGUID],
[distinguishedName],
[adForest],
[adDomain],
[accountExpires],
[adminCount],
[cn] ,
[company],
[description],
[displayName],
[division],
[employeeID],
[change_type]
)
select
INSERTED.[objectGUID],
INSERTED.[distinguishedName],
INSERTED.[adForest],
INSERTED.[adDomain],
INSERTED.[accountExpires],
INSERTED.[adminCount],
INSERTED.[cn] ,
INSERTED.[company],
INSERTED.[description],
INSERTED.[displayName],
INSERTED.[division],
INSERTED.[employeeID],
'Add'
From inserted
END
END
GO
I am trying to create a table-valued function in SQL Server. My issue is that I can't find the right syntax for the SQL. I keep getting errors. I don't know if it is possible to use an execute() method in a table-valued function. I have tried declaring and setting the variables and also using the execute method in an oridinary sql query and it works.
My SQL:
CREATE FUNCTION SortRoutePartByDay
(
#date datetime
)
RETURNS TABLE
AS
Begin
declare #cmdtext varchar(max)
declare #Daynameofweek varchar(10)
set #Daynameofweek = datename(weekday, #date)
set #cmdtext = 'select * from RoutePartPart where ' +#Daynameofweek+' =1';
RETURN
(
execute(#cmdtext)
)
GO
My error so far is:
Msg 156, Level 15, State 1, Procedure SortRoutePartByDay, Line 21
Incorrect syntax near the keyword 'execute'.
Msg 102, Level 15, State 1, Procedure SortRoutePartByDay, Line 23
Incorrect syntax near ')'.
RoutePartPart DDL:
CREATE TABLE [dbo].[RoutePartPart](
[RouteID] [int] NOT NULL,
[RoutePartNo] [smallint] NOT NULL,
[RoutePartPartNo] [smallint] NOT NULL,
[PickupAreaGrpID] [int] NULL,
[DeliveryAreaGrpID] [int] NULL,
[Monday] [bit] NULL,
[Tuesday] [bit] NULL,
[Wednesday] [bit] NULL,
[Thursday] [bit] NULL,
[Friday] [bit] NULL,
[Saturday] [bit] NULL,
[Sunday] [bit] NULL,
[Pickup] [bit] NULL,
[Delivery] [bit] NULL,
[Types] [varchar](10) NULL
) ON [PRIMARY]
I think using dynamic SQL is unnecessary in your case, so try this one -
Query:
CREATE FUNCTION SortRoutePartByDay
(
#date DATETIME
)
RETURNS TABLE
AS RETURN
SELECT *
FROM dbo.RoutePartPart
WHERE DATENAME(weekday, #date) = 1
Small info:
Functions on SQL Server are not the same as stored procedures, they have several limitations on the things that can be done. For example, you can't use dynamic SQL.
Update:
CREATE FUNCTION SortRoutePartByDay
(
#date DATETIME
)
RETURNS TABLE
AS RETURN
SELECT p.*
FROM dbo.RoutePartPart p
CROSS JOIN (
SELECT [WeekDay] = DATENAME(weekday, #date)
) t
WHERE ([WeekDay] = 'Monday' AND [Monday] = 1)
OR ([WeekDay] = 'Tuesday' AND [Tuesday] = 1)
OR ([WeekDay] = 'Wednesday' AND [Wednesday] = 1)
OR ([WeekDay] = 'Thursday' AND [Thursday] = 1)
OR ([WeekDay] = 'Friday' AND [Friday] = 1)
OR ([WeekDay] = 'Saturday' AND [Saturday] = 1)
OR ([WeekDay] = 'Sunday' AND [Sunday] = 1)