Running 'SET' command in SQL, prior to SELECT

北城余情 提交于 2019-12-31 04:23:04

问题


Following this question, I need to add something like

SET LANGUAGE German;

before my SELECT query. I am running the query in an ASP/VBScript web environment, which basically limits me, as far as I know, to a single query at a time. If I run a query such as -

SET LANGUAGE German; SELECT.....

I get a no results message, because the 'data' returned is from the SET query and not the SELECT that follows it.

Is there anything that can be done to run the SET and the SELECT together in the ASP/VBScript environment?

UPDATE: As per Lankymarts suggestion:

set rs = SERVER.CreateObject("ADODB.recordset")
rs.open sql, conn, 1, 2

Do While (rs.State = 0 Or rs Is Not Nothing)  // also tried: Do While (rs.State = 0 AND rs Is Not Nothing) 
 Set rs = rs.NextRecordset
Loop

do while not rs.eof
  response.write ...

UPDATE 2:
Now that the closed recordset issue is solved, I am still not getting rows from the main recordset. This is my VBScript code, below. There are definitely results (because 21/feb/16 - was on Sunday, and i have matching records for this) - but they are not being displayed. In fact even displaying via SSMS sometimes i dont get the results - maybe its getting all confused with the language changes?

    sql = " SET LANGUAGE German; "
    sql = sql & " SELECT [tblstudentrakazot].studentid, firstname, lastname, tblRakezetSchedule.* FROM tblRakezetSchedule"
    sql = sql & " INNER join [tblstudentrakazot] on [tblstudentrakazot].scheduleID = tblRakezetSchedule.scheduleid "
    sql = sql & " INNER join tblstudents on [tblstudentrakazot].studentid = tblstudents.studentid"
    sql = sql & " WHERE CONVERT(int,scheduleday) = datepart(d,convert(datetime,'" & cleanSQL(planneddate) & "',103)) AND "
    sql = sql & " tblRakezetSchedule.rakezetID = " & CleanSQL(x_rakezetID)
    sql = sql & " ORDER BY replace(scheduletimefrom, ':', '')"

    response.Write("### " & sql)
    set rs = SERVER.CreateObject("ADODB.recordset")
    rs.open sql, conn, 1, 2

    Do While rs.State = 0 And Not rs Is Nothing
     Set rs = rs.NextRecordset
    loop 


    do while not rs.eof 
       ' we now proceed to loop through the actual result recordset : studentid, firstname etc... 

By the way - does the language remain in German after the query has run, or does it return to its default language?

I guess what i need here is a language setting whose default is dd/mm/yyyy (because of other legacy requirements in the system) and one that the DATEFIRST is Sunday (1).

ALSO: I tried to make a stored procedure, as such:

    ALTER PROCEDURE [dbo].[procListRakezetSlotsByDay] @planneddate nvarchar(10),  @rakezetID int

    AS
    BEGIN

        SET NOCOUNT ON;
        SET LANGUAGE German;

    SELECT [tblstudentrakazot].studentid, firstname, lastname, tblRakezetSchedule.* FROM tblRakezetSchedule
    INNER join [tblstudentrakazot] on [tblstudentrakazot].scheduleID = tblRakezetSchedule.scheduleid 
    INNER join tblstudents on [tblstudentrakazot].studentid = tblstudents.studentid
    WHERE CONVERT(int,scheduleday) = datepart(d,convert(datetime,@planneddate,103)) AND tblRakezetSchedule.rakezetID = @rakezetID
    ORDER BY replace(scheduletimefrom, ':', '')


    END

and then run it:

    DECLARE @return_value int

    EXEC    @return_value = [dbo].[procListRakezetSlotsByDay]
            @planneddate = N'28/2/2016',
            @rakezetID = 182

    SELECT  'Return Value' = @return_value

    GO

and here too, it returns no results - even within SSMS... I am VERY confused. thanks to all who have helped so far.


回答1:


This is a misconception. Neither ASP/VBScript limits you, the limit is imposed by the provider ADODB uses to perform the command. In terms of SQL Server though there is no limit (I know of) when executing a command that contains multiple queries.

First

SET LANGUAGE German;

isn't really a returning query but the Provider will return it as a closed ADODB.Recordset object, which isn't ideal but there is a simple fix.

SET NOCOUNT ON;

Will inhibit DONE_IN_PROC messages from being sent to say the executing line was successful which is interpreted by ADODB as a closed ADODB.Recordset object.

Another way to deal with this but not as straight-forward as SET NOCOUNT ON is to use the NextRecordSet() method of the ADODB.Recordset object to step through the various resultsets until you find the actual query result.

Assuming rs is our starting ADODB.Recordset object

Do While (rs.State = adStateClosed And Not rs Is Nothing)
 Set rs = rs.NextRecordset
Loop

will return the first ADODB.Recordset object that isn't in the closed state.

From MSDN - NextRecordset Method (ADO)

As long as there are additional results and the Recordset containing the compound statements is not disconnected or marshaled across process boundaries, the NextRecordset method will continue to return Recordset objects. If a row-returning command executes successfully but returns no records, the returned Recordset object will be open but empty. Test for this case by verifying that the BOF and EOF properties are both True. If a non–row-returning command executes successfully, the returned Recordset object will be closed, which you can verify by testing the State property on the Recordset. When there are no more results, recordset will be set to Nothing.



来源:https://stackoverflow.com/questions/35869099/running-set-command-in-sql-prior-to-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!