I'm having a problem returning an integer value from an identity column where I only want the values greater than 5 which will be used in another select. The code I have so far:
CREATE FUNCTION [dbo].[fn_Get_Dragon_ID]()
RETURNS INT
AS
BEGIN
DECLARE @I INT
SELECT @I = Dragon_ID
FROM Dragons
WHERE Dragon_ID > 5
RETURN @I
END
-- How it's being used:
CREATE PROCEDURE [dbo].[sp_Discovered_Dragons]
AS
SELECT
dbo.fn_Get_Dragon_ID() AS Dragon_ID,
[dbo].[fn_Set_Tracker](ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 7 + 1)
AS Assigned_Tracker
FROM
[dbo].[stDrgnInbnd0]
The output I expected:
Dragon_ID Assigned_Tracker
6 Hiccup
7 Camicazi
8 Hiccup
9 Hiccup
10 Tuffnut
11 Snotface Snotlout
.
.
.
What I'm actually getting:
Dragon_ID Assigned_Tracker
305 Hiccup
305 Camicazi
305 Hiccup
305 Hiccup
305 Tuffnut
305 Snotface Snotlout
.
.
.
I've searched the site and Google'd my brains out but I must be stating the issue incorrectly. Thanks for any assistance.
UPDATE: The Dragon_ID column is an identity column - (1,1), which I am attempting to get the rows higher than 5 (the row count of the initial table) and the row id's after an insert.
@Gordon Linoff: I tried earlier to use the code inline as you suggested however :
declare @I int
SELECT
(SELECT @I as Dragon_ID
FROM Dragons
WHERE Dragon_ID > 5),
stDrgnInbnd0.Dragon_Name AS Dragon_Type,
stDrgnInbnd0.Sighting_Date,
stDrgnInbnd0.Sighting_Location,
stDrgnInbnd0.Pack_Status,
stDrgnInbnd0.Day_OR_Night_Sleep,
[dbo].[fn_Set_Tracker](ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 7 + 1)
AS Assigned_Tracker
FROM
[dbo].[stDrgnInbnd0]
And got the following error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Thanks again.
fn_Set_Trackerfunction in the same way perhaps you can pass some value, compute in the function and return. Unless you show the input schema and the requirement coming up with a solution is difficult.