我正在尝试获取一个唯一的ID号,并且我已经编写了以下SQL Server函数来执行此操作。我将为该函数提供当前管理员的ID,它将位于左侧的第一个数字上。现在我希望它只返回一个非常独特的数字。我已经使用了多个,如果这样,那就不应该匹配。我希望在表orderdetailstb中没有orderid时。如果有的话应该+1。
USE [ResturantManagementSystem]
GO
/****** Object: UserDefinedFunction [dbo].[we] Script Date: 11/11/2016 11:48:59 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [dbo].[UniqueOrderId] (@currentAdminid int)
RETURNS int
AS BEGIN
declare @UniqueOrderId int
if (select orderid from OrderDetailsTB) is null
return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112),right('1',1)) from Admin)
else
if
(select max(CONVERT(int,getdate(),112)) from OrderDetailsTB)>CONVERT(int,getdate(),112)
return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112),right('1',1)) from Admin)
else
return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112),right(select max(orderid)+1 from OrderDetailsTB),3) from Admin)
return @UniqueOrderId
END
问题在于
给出错误return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112),right(select max(orderid)+1 from OrderDetailsTB),3) from Admin)
错误是
消息156,级别15,状态1,过程UniqueOrderId,第12行不正确 关键字'select'附近的语法。 Msg 102,Level 15,State 1, 过程UniqueOrderId,第12行')'附近的语法不正确。
我该怎么做以及我是否适合使用某个功能,或者我应该将其移向存储过程。
答案 0 :(得分:1)
你可以试试这个:
library(data.table)
melt(setDT(df1), measure = patterns("Style", "Price"),
value.name = c("Style", "Price"))[df2, on = c("Name", "Style")][, variable := NULL][]
# Name Style Price
#1: Gary AB 300
#2: Johnson A 300
#3: Marsha C 500
#4: Watson B 200
#5: Emma BC 600
我们的想法是使用SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [dbo].[UniqueOrderId] (@currentAdminid int)
RETURNS int
AS BEGIN
declare @UniqueOrderId int
if (select orderid from OrderDetailsTB) is null
return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112),right('1',1)) from Admin)
else
if
(select max(CONVERT(int,getdate(),112)) from OrderDetailsTB)>CONVERT(int,getdate(),112)
return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112),right('1',1)) from Admin)
else
return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112)
,right(maxOrderId,3))
from Admin
cross apply (select max(orderid)+1 from OrderDetailsTB) ds(maxOrderId)
)
return @UniqueOrderId
END
或outer
申请转换内联cross
声明:
select
为:
return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112),right(select max(orderid)+1 from OrderDetailsTB),3) from Admin)