Author |
Topic |
Gremlin
General Help Moderator
New Zealand
7528 Posts |
Posted - 05 January 2005 : 16:07:02
|
Benefit is being able to setup a database without using EM or similar.
If your providing an application to people that uses SP's you do need a way to be able to load the app into their database potentially without EM access (not all hosts give you this access, and many ISP's block TCP/1433).
Other ASP apps I've seen create the SP's via scripts so I know it should work.
As for limitations, really the biggest I forsee is the lack of information errors messages you'll get back when trying to execute the SQL statement from ASP could be a bit of a pain to debug if it's not working and to put appropriate error handling code in to your ASP script. |
Kiwihosting.Net - The Forum Hosting Specialists
|
Edited by - Gremlin on 05 January 2005 16:08:14 |
|
|
dayve
Forum Moderator
USA
5820 Posts |
Posted - 05 January 2005 : 16:52:55
|
You can set up a database without having to use a Stored Procedure, using simple SQL statements.
I've been doing a lot of Googling on this and have not once come up with an example of CREATING stored procedure via ASP, only EXECUTING. However, since you have seen this first hand yourself, I am going to take it as fact. I am going to create an ASP application right now and attempt to create a stored procedure via the code. I'm very interested in witnessing any benefits that may come from this besides another method of creating the stored procedure itself because logic tells me that there will be a performance degradation doing thing, but maybe nothing significant enough in small database applications to notice. |
|
|
|
Da_Stimulator
DEV Team Forum Moderator
USA
3373 Posts |
Posted - 05 January 2005 : 16:59:27
|
I dont see how it would degrade performance, only help it. Thats why we use stored procs right? performance.
The purpose is not to use the code to create a stored proc *alot*, but to run it once (to create the proc), then to use the proc throughout the code...
I'm testing some code now that detects whether or not the stored proc trying to be created exists, and if not, to create it (see this thread) |
-Stim |
Edited by - Da_Stimulator on 05 January 2005 16:59:47 |
|
|
dayve
Forum Moderator
USA
5820 Posts |
Posted - 05 January 2005 : 17:07:39
|
yes, Executing a Stored Procedure will put the burden on the server and not the client therefore making it perform better, but using ASP to create a stored procedure is more steps than necessary IMO. I am still testing.
If this does work, I can't imagine it handling complicated/lengthy Stored Procedures, only simplistic ones at best. Variables in ASP are limited to fixed length/size (thought I don't know what that limit is right now). If your Stored Procedure is only a few lines and it can be accomplished using SQL I'd surely go that route, but again, I'm still doing some of my own testing.
BTW, did you get yours to actually work? |
|
|
|
Da_Stimulator
DEV Team Forum Moderator
USA
3373 Posts |
Posted - 05 January 2005 : 17:28:14
|
No, havnt got it to work yet... still playing around
When I try to run the code, I get the following Error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the variable '@pTable'.
/test.asp, line 15
And this is the code I'm trying to run (excuse the simplicity, just trying to get it to work right now)
strsql = "CREATE Procedure sp_testProc " & _
" @pField varchar(20), " & _
" @pTable varchar(20)" & _
" As " & _
" Select @pField FROM @pTable ORDER BY @pField ASC " & _
" Go"
dataConn.Execute(strsql)
|
-Stim |
Edited by - Da_Stimulator on 05 January 2005 17:31:33 |
|
|
dayve
Forum Moderator
USA
5820 Posts |
Posted - 05 January 2005 : 17:35:16
|
Well, I'll be ****ed. I created a Stored Procedure via ASP, although it was not a complicated one, it worked. Man my foot is tasty! . |
|
|
|
Da_Stimulator
DEV Team Forum Moderator
USA
3373 Posts |
Posted - 05 January 2005 : 17:36:53
|
good, maybe now that you're foot's partially removed, have any troubleshooting hints for me? lol, I cant seem to get this to work. |
-Stim |
|
|
dayve
Forum Moderator
USA
5820 Posts |
Posted - 05 January 2005 : 17:41:34
|
I did not add the GO statement in mine because it gave me an error, so I omitted it and when I looked at the stored procedure on the sql server, Go was automatically inserted. So remove the GO statement and see what happens.
PS. based on what you're doing, I'd still use SQL. |
|
Edited by - dayve on 05 January 2005 17:42:26 |
|
|
Da_Stimulator
DEV Team Forum Moderator
USA
3373 Posts |
Posted - 05 January 2005 : 17:43:29
|
Same error... hmmmm
quote:
PS. based on what you're doing, I'd still use SQL.
Thats not related at all to what I plan on doing with this if it works... this is just for testing purposes. |
-Stim |
Edited by - Da_Stimulator on 05 January 2005 17:44:34 |
|
|
dayve
Forum Moderator
USA
5820 Posts |
Posted - 05 January 2005 : 17:48:39
|
I used Open instead of Execute, but I will try with Execute since I think that is the preferred syntax
Dim conn, rs, strSQL
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Data Source=SQLSVR1;database=******;uid=sa;pwd=********;"
set rs = Server.CreateObject("ADODB.Recordset")
strSQL = "CREATE PROCEDURE sp_GetID @location varchar(15) AS SELECT TENANT_ID, HEAD_L_NAME, HEAD_F_NAME, PROJECT_ID FROM ten_mast where project_id = @location"
rs.Open strSQL, conn
set rs = Nothing
|
|
|
|
Da_Stimulator
DEV Team Forum Moderator
USA
3373 Posts |
Posted - 05 January 2005 : 17:52:54
|
I tried using the rs.Open method (pretty much exactly the code you posted), and it threw the same exact error.
This is the entirety of my code
strConnString = "driver={SQL Server};server=sqlxx.xxxxx.net;uid=xxxx;pwd=xxxx;database=xxxx"
set dataConn = Server.CreateObject("adodb.connection")
dataConn.Open strConnString
'#### Lets test out this stored procedure thing
if request.querystring("s") = "create" then
strsql = "CREATE Procedure sp_testProc " & _
" @pField varchar(20), " & _
" @pTable varchar(20)" & _
" As " & _
" Select @pField FROM @pTable ORDER BY @pField ASC "
Set rs = Server.createObject("adodb.recordset")
rs.Open strsql, dataConn
rs.Close
Set rs = nothing
response.write("Stored Procedure Created")
end if
dataConn.Close
set dataConn = nothing
|
-Stim |
Edited by - Da_Stimulator on 05 January 2005 17:54:15 |
|
|
D3mon
Senior Member
United Kingdom
1685 Posts |
Posted - 05 January 2005 : 18:19:43
|
I think the problem is here:
strsql = "CREATE Procedure sp_testProc " & _ " @pField varchar(20), " & _ " @pTable varchar(20)" & _ " As " & _ " Select @pField FROM @pTable ORDER BY @pField ASC "
You can't specifiy table/fields with SQL @variables (even with normal SP's) like that. Just put in the values that the stored procedure would normal request in the select statement:
strsql = "CREATE Procedure sp_testProc " & _ " @pField varchar(20), " & _ " @pTable varchar(20)" & _ " As " & _ " Select [my_fields] FROM [my_table] ORDER BY [my_field] ASC "
|
Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod "In war, the victorious strategist only seeks battle after the victory has been won" |
Edited by - D3mon on 05 January 2005 18:20:23 |
|
|
Podge
Support Moderator
Ireland
3775 Posts |
|
Podge
Support Moderator
Ireland
3775 Posts |
Posted - 05 January 2005 : 19:14:19
|
This example might help.
Create a text file called create_sp.sql containing this text
quote:
CREATE PROCEDURE ZCommunity_VotingGetPollsForWebBox2 @InCommunity_ID int, @InSection_ID int, @InUsername nvarchar(50) AS DECLARE @DefaultRoles nvarchar(256)
SELECT DISTINCT p.Poll_ID FROM Community_VotingPolls p LEFT JOIN Community_VotingPollSections s ON s.Poll_ID = p.Poll_ID INNER JOIN Community_VotingPollRoles r ON r.Poll_ID = p.Poll_ID WHERE Community_ID = @InCommunity_ID AND IsActive=1 AND (IsGlobal=1 OR (s.Section_ID = @InSection_ID)) AND (r.Rolename = 'Community-Everyone' OR r.Rolename IN (SELECT 'Community-Authenticated' AS Role FROM Community_Users WHERE User_Username=@InUsername) OR r.Rolename IN (SELECT UserRoles_Rolename FROM Community_UsersInRoles WHERE UserRoles_Username=@InUsername)) AND (StartDate is null OR StartDate <= getUtcdate()) AND (EndDate is null OR EndDate >= getUtcdate())
Note: Don't include the "GO" statement usually at the end of an sp.
This is the code for an asp file. The stored procedure it creates is a copy of one from Microsofts dotnet Community Starter Kit.
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Dim objTextStream
strFileName = server.MapPath("create_sp.sql")
const fsoForReading = 1
response.Write (strFileName)
If objFSO.FileExists(strFileName) then
'The file exists, so open it and output its contents
Set objTextStream = objFSO.OpenTextFile(strFileName, fsoForReading)
sql = objTextStream.ReadAll
objTextStream.Close
Set objTextStream = Nothing
Else
'The file did not exist
Response.Write strFileName & " was not found."
End If
'Clean up
Set objFSO = Nothing
strConnString = "Provider=SQLOLEDB;Data Source=127.0.0.1;uid=username;pwd=password;database=communitystarterkit"
set my_Conn = Server.CreateObject("ADODB.Connection")
my_Conn.Open strConnString
my_conn.Execute sql
my_conn.Close
Set my_conn = Nothing
%>
|
Podge.
The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)
My Mods: CAPTCHA Mod | GateKeeper Mod Tutorial: Enable subscriptions on your board
Warning: The post above or below may contain nuts. |
Edited by - Podge on 05 January 2005 19:15:47 |
|
|
Da_Stimulator
DEV Team Forum Moderator
USA
3373 Posts |
Posted - 05 January 2005 : 22:18:01
|
Sweet I got it to work!
Some more experimentation and I'll be set... time to start playing with ALTER and DROP |
-Stim |
|
|
Topic |
|