ASP calls the MSSQL stored procedure with parameters and returns the temporary table recordset

Source: Internet
Author: User
Tags insert mssql return

A few days ago, when developing a system platform, the Arisisi Web Team (alixixi.com) needed to call several fields of multiple tables through a stored procedure, merge them into a temporary table, and sort them back to an ASP call.

Below, I share the experience:

1, stored procedure part:

The following are part of the stored procedure:
CREATE PROCEDURE [dbo]. [Alixixi]
@sNum int,
@sOrder varchar (20)
As
SET Nocount ON
CREATE TABLE #btable (ID int,channelid int,classid int,title varchar (), ImageUrl varchar (), htmlfiledate varchar ( ), Addtime datetime)
Execute (' INSERT into #btable select top ' + @sNum + ' id,fid,cid,title,imageurl,myurl,writetime from ali_tb1 where Isbest=1 ' )
Execute (' INSERT into #btable select top ' + @sNum + ' id,fid,cid,topic,photourl,siteurl,addtime from ali_tb2 where Isbest=1 ' )
Execute (' SELECT * from #btable ORDER BY ' + @sOrder + ' desc ')
Go

The function of this stored procedure is to establish a stored procedure named Alixixi, with two parameters snum and Sorder, which can be flexibly controlled to display the number of records returned and the sorting method.

In the MSSQL Query Analyzer, we can execute the stored procedure in this form to return two table each 5 records (altogether 10):

EXEC Alixixi 5, ' Addtime '

Below, we talk about the methods called in ASP (conn for database connection, please check your environment, the relevant database connection code is not listed here).

The following is a reference fragment:
Dim cmd
Set Rs =server.createobject ("ADODB.") Recordset ")
Set CMD = Server.CreateObject ("Adodb.command")
Set Cmd.activeconnection=conn
cmd.commandtext= "Alixixi"
Cmd.Parameters.append cmd.createparameter ("@sNum", 3,1,4,50)
Cmd.Parameters.append cmd.createparameter ("@sOrder", 200,1,20, "Addtime")
Cmd.commandtype=4
Set Rs=cmd.execute
Set CMD = Nothing
If not rs.eof Then
Do as not rs.eof
Response.Write Rs (0) & "<br>"
Rs.movenext
Loop
Else
Response.Write "No Record"
End If
Set rs=nothing

By using the above method, we can implement ASP to invoke the MSSQL stored procedure with parameter, and return the temporary table recordset.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.