0

I am trying to get a unique id number and i have wrote the following SQL Server function to do it. I will give the function the ID of the current admin and it will be on the first digit on the left. Now i want it to return a very unique number only. I have used multiple if so that there shouldn't be a match. I want that when there is no orderid in the table orderdetailstb. And if there is it should +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

the problem is its giving error at

return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112),right(select max(orderid)+1 from OrderDetailsTB),3) from Admin)

the error is

Msg 156, Level 15, State 1, Procedure UniqueOrderId, Line 12 Incorrect syntax near the keyword 'select'. Msg 102, Level 15, State 1, Procedure UniqueOrderId, Line 12 Incorrect syntax near ')'.

How can i do it and if it is appropriate for me to use a function or should i move it towards a stored procedure.

0

1 Answer 1

1

Could you try this:

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

The idea is to use outer or cross apply in order to transform inline select statement:

return (select concat(left(@currentAdminid,1),CONVERT(int,getdate(),112),right(select max(orderid)+1 from OrderDetailsTB),3) from Admin) 

to:

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)

        )
Sign up to request clarification or add additional context in comments.

6 Comments

Side note: (select max(CONVERT(int,getdate(),112)) from OrderDetailsTB)>CONVERT(int,getdate(),112) doesn't make sense.
Sorry, that was meant as a comment to the question.
Well i want that everyday a new code needs to be generated.
in the end you have put ds? what is that for?
This is just alias that we are giving to the cross apply data - you can put anything you want
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.