| Author |
Topic |
|
mattdwen
Starting Member
4 Posts |
Posted - 2007-01-02 : 19:30:10
|
| I was wondering if it's possible to 'call' a stored procedure from another one, as part of the select.e.g.declare @itemid intselect @itemid = item.id, (exec item_checkstatus @itemid) as statusfrom itemsWhere 'item_checkstatus' is returning a int value.Cheers |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-01-02 : 19:33:33
|
| That isn't possible. You can do this with user-defined functions though. If you post the code for your sproc, we can tell you if it can convert to a UDF.Tara Kizer |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2007-01-02 : 20:40:36
|
can if item_checkstatus takes output param.SET NOCOUNT ONdeclare @itemid intdeclate @checkstatus intSELECT @itemid = idFROM itemsExec item_checkstatus @itemid, @checkstatus outputSELECT @itemid, @checkstatusGO also check BOL for sp_executesql which is probably the way u need to go. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-02 : 21:15:53
|
| Ok, we didn't get through...Post the code for item_checkstatus sproc.Peter LarssonHelsingborg, Sweden |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-01-02 : 22:01:17
|
| Even if the stored procedure has an output parameter or returns it, you still can't execute it in a SELECT statement.Tara Kizer |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2007-01-02 : 22:20:44
|
| true, but the desired result set CAN be returned in SP |
 |
|
|
mattdwen
Starting Member
4 Posts |
Posted - 2007-01-02 : 22:32:56
|
| Aha, that kinda makes sense.The main procedure returns a set of booked items for a specific booking:CREATE PROCEDURE ScheduleBookingItem_GetByBooking(@ixBooking Int)ASSELECT ScheduleBookingItem.*, !AND THE STATUS!FROM ScheduleBookingItemINNER JOIN ScheduleBooking ON ScheduleBookingItem.ixBooking = ScheduleBooking.ixBookingWHERE ixBooking = @ixBooking AND nStatus = 1 AND fRemoved = 0The secondary procedure checks to see if the items returned from the first procedure are double booked anywhere, and if so, returns a conflict.CREATE PROCEDURE ScheduleBookingItem_CheckConflict(ixItem Int, ixBooking Int, sCategory NVarChar(64), dtmStart DateTime, dtmEnd DateTime)ASIF EXISTS ( SELECT ScheduleBookingItem.ixBookingItem FROM ScheduleBookingItem INNER JOIN ScheduleBooking ON ScheduleBookingItem.ixBooking = ScheduleBooking.ixOriginal WHERE ScheduleBookingItem.ixItem = @ixItem AND ScheduleBookingItem.sCategory = @sCategory AND ScheduleBookingItem.nStatus = 1 AND ScheduleBookingItem.ixBooking <> @ixBooking AND (ScheduleBookingItem.intBookingStatus <> -1 OR ScheduleBookingItem.intBookingStatus <> 4) AND ScheduleBookingItem. fRemoved = 0 AND ScheduleBooking.nStatus = 1 AND ScheduleBooking.fCanceled = 0 AND ScheduleBooking.dtmStart < @dtmEnd AND ScheduleBooking.dtmEnd >= @dtmStart )RETURN -2ELSERETURN (SELECT intStatusFROM ScheduleBookingItemWHERE ixItem = @ixItem)So it's a quite a complex check, but I know it works. Basically if it finds something it returns a conflict code, otherwise returns what the code is normally.All the parameters that get passed to the CheckConflict sproc are available in the initial lookup. |
 |
|
|
mattdwen
Starting Member
4 Posts |
Posted - 2007-01-02 : 22:42:30
|
| I'm guessing by the function comments, if i turn the second sproc into a function, I can just call it like the AVG() function?I've never written a function before, but i'm guessing it's pretty similar. |
 |
|
|
mattdwen
Starting Member
4 Posts |
Posted - 2007-01-02 : 22:56:42
|
| Yup, I have it sorted thanks. I'll attach the code so you can still point out if there's a better way to do it.CREATE PROCEDURE ScheduleBookingItem_GetByBooking( @ixBooking Int)ASDECLARE @ReturnValue NVarChar(64)SELECT ScheduleBookingItem.*, dbo.ScheduleBookingItem_CheckConflict(ScheduleBookingItem.ixComp, @ixBooking, ScheduleBookingItem.sCategory, ScheduleBooking.dtmStart, ScheduleBooking.dtmEnd) AS StatusFROM ScheduleBookingItem INNER JOIN ScheduleBooking ON ScheduleBookingItem.ixBooking = ScheduleBooking.ixOriginalWHERE ScheduleBookingItem.ixBooking = @ixBooking AND ScheduleBookingItem.nStatus = 1 AND ScheduleBookingItem.fRemoved = 0 AND ScheduleBooking.nStatus = 1CREATE FUNCTION ScheduleBookingItem_CheckConflict( @ixItem Int, @ixBooking Int, @sCategory NVarChar(64), @dtmStart DateTime, @dtmEnd DateTime)RETURNS NVarChar(64)ASBEGIN DECLARE @sStatus NVarChar(64) IF EXISTS ( SELECT ScheduleBookingItem.ixBookingItem FROM ScheduleBookingItem INNER JOIN ScheduleBooking ON ScheduleBookingItem.ixBooking = ScheduleBooking.ixOriginal WHERE ScheduleBookingItem.ixComp = @ixItem AND ScheduleBookingItem.sCategory = @sCategory AND ScheduleBookingItem.nStatus = 1 AND ScheduleBookingItem.ixBooking <> @ixBooking AND (ScheduleBookingItem.intBookingStatus <> -1 OR ScheduleBookingItem.intBookingStatus <> 4) AND ScheduleBookingItem. fRemoved = 0 AND ScheduleBooking.nStatus = 1 AND ScheduleBooking.fCanceled = 0 AND ScheduleBooking.dtmStart < @dtmEnd AND ScheduleBooking.dtmEnd >= @dtmStart ) SET @sStatus = 'Conflict' ELSE BEGIN SELECT @sStatus = sStatus FROM ScheduleBookingItem INNER JOIN ScheduleStatus ON ScheduleBookingItem.intBookingStatus = ScheduleStatus.intCode WHERE ixComp = @ixItem AND nStatus = 1 END RETURN @sStatusEND |
 |
|
|
|