Monday, March 19, 2012
getting results of sp_pkeys in a recordset
rsPrimaryKey.Open "exec sp_pkeys 'dbo.tmpQuestionnaireDumpReport'"
doesn't work
rsPrimaryKey.Open "sp_pkeys 'dbo.tmpQuestionnaireDumpReport'"
doesn't work either.
I know in Query Analyzer, sp_pkeys 'dbo.tmpQuestionnaireDumpReport'
works.
Please help.Check this one:
<% LANGUAGE="VBScript" %>
<% Response.Expires =0 %>
<HTML>
<HEAD>
<TITLE>
</TITLE>
</HEAD>
<BODY>
<%
Session("db")="DSN=homebudget;UID=sa;PWD=;"
Set Session("dbcon") = Server.CreateObject("ADODB.Connection")
Session("dbcon").Open Session("db")
set execommand = Server.CreateObject("ADODB.Command")
execommand.CommandText = "sp_pkeys"
execommand.ActiveConnection = Session("dbcon")
execommand.CommandType = &H0004
execommand.Parameters.Append execommand.CreateParameter("table",200,&H0001,8000)
execommand("table")="test"
set rs=execommand.execute
response.write rs(0)+"<br>"
response.write rs(1)
rs.close
set rs=nothing
%>
</BODY>
</HTML>|||Thank you Snail, but I tried that already. The command object doesn't work with sp_pkeys in VB. It would work with any other stored procedure that you define yourself.
This is what worked for me:
select Column_Name
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS t
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
ON t.Constraint_Name = c.Constraint_Name
where t.table_name = 'lkuDataAudit'
AND t.Constraint_Type = 'PRIMARY KEY'|||Originally posted by archnam
Thank you Snail, but I tried that already. The command object doesn't work with sp_pkeys in VB. It would work with any other stored procedure that you define yourself.
This is what worked for me:
select Column_Name
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS t
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
ON t.Constraint_Name = c.Constraint_Name
where t.table_name = 'lkuDataAudit'
AND t.Constraint_Type = 'PRIMARY KEY'
It works in ASP it has to work in VB (I did not test it in VB).
You have to run command as stored procedure for sp_pkeys.
getting recordset via ado/sproc
I have stored procedure with select statement that returns rows when executed from query analyser:
CREATE procedure ps_tblMackVendors_Select_test
AS
set quoted_identifier on
declare @.strsql nvarchar(300)
set @.strsql = 'Select * from tblMackVendors
exec (@.strsql)
GO
When I execute following bit of code from Access, the set rst = cmd.execute runs ok, but the rst.movefirst gets error 3704, Operation is not allowed when the object is closed.
Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Set cmd = New ADODB.Command
cmd.ActiveConnection = mcnnSQLDB
cmd.CommandText = "ps_tblMackVendors_Select_test"
cmd.CommandType = adCmdStoredProc
Set rst = cmd.Execute
rst.MoveFirst
I have tested the stored procedure & the connection using an output parameter, doing a cmd.execute (without the rst..) & the value is retrieved by access okprobably you need to rethink your app structure before you go too far with it. i bet your rst will successfully movefirst if you just do a straight select from your sp.|||My question would be why do you have
CREATE procedure ps_tblMackVendors_Select_test
AS
set quoted_identifier on
declare @.strsql nvarchar(300)
set @.strsql = 'Select * from tblMackVendors
exec (@.strsql)
GO
instead of,...
CREATE procedure ps_tblMackVendors_Select_test
AS
Select * from tblMackVendors
GO
???|||well, i understand he's just experimenting, getting ready for the real stuff, but i'm saying "don't go that route, there is a sign there - no outlet!"|||What happens if you open a recordset using a sql string instead of a stored procedure ? Also, you should debug your vb code and view the locals to see what is going on with the rst object.|||my sp uses string for qry because I want to build the sql from values passed to the sp in the final version.
I did just try it without the string for the sql & it worked - good idea. But it does work with the string when I call the sp from query analyzer. Why do you think there is a difference ?|||multiple recordsets is one option,...
using sp_executesql instead of exec might help.
personally I'd really avoid this method of doing things though... if you are going to use dynamic sql then use it in the application, not in a stored proc.
Monday, March 12, 2012
Getting records and output parameter from ASP
I can get either but not both. (ie. if there is a select then there is no output parameter)
The stored procedure is:
ALTER PROCEDURE test
(
@.Msgs nvarchar(150) OUTPUT
)
As
declare @.selCount int
SELECT folderLocation, externalDocsID from externalDocs
set @.selCount = @.@.ROWCOUNT
IF @.@.ROWCOUNT = 0
BEGIN
SET @.Msgs = 'No folders meet this selection criteria'
RETURN
END
else
BEGIN
SET @.Msgs = @.selCount
END
return
-------------------------
the asp code is
Dim cnnStoredProc ' Connection object
Dim cmdStoredProc ' Command object
Dim rstStoredProc ' Recordset object
Dim folderText
Set cnnStoredProc = Server.CreateObject("ADODB.Connection")
cnnStoredProc.Open db
' get the correct records for a page
Set cmdStoredProc = Server.CreateObject("ADODB.Command") ' Create Command object we'll use to execute the SP
cmdStoredProc.ActiveConnection = cnnStoredProc ' Set our Command to use our existing connection
cmdStoredProc.CommandText = "test" ' Set the SP's name and tell the Command object
cmdStoredProc.CommandType = adCmdStoredProc
cmdStoredProc.Parameters.Refresh
' ---- SET PARAMETERS --GET A PAGE WORTH OF RECORD---------------------
set prop=ADODB.Parameter
cmdStoredProc.Parameters("@.functionCode").Value = "L" 'functionCode
cmdStoredProc.Parameters("@.customerID").Value = customerId 'CustomerID
if searchCriteria <> "" then
cmdStoredProc.Parameters("@.searchcriteria").Value = searchCriteria 'search string
end if
if showAssigned = "Y" then
cmdStoredProc.Parameters("@.excludeDefined").Value = "Y" '
end if
set rstStoredProc = cmdStoredProc.Execute
'
response.write("msgs=" & cmdStoredProc("@.Msgs")) ' <<<<<<< THIS DOESN'T WORK------------------------------
I can enumerate through the recordset but the output parameter is blank.
Any Ideas
Thanks
Quote:
Originally Posted by Mike Lester
I have a need for a stored procedure to return a recordset AND an output parameter that contains the count of records in the recordset.
I can get either but not both. (ie. if there is a select then there is no output parameter)
The stored procedure is:
ALTER PROCEDURE test
(
@.Msgs nvarchar(150) OUTPUT
)
As
declare @.selCount int
SELECT folderLocation, externalDocsID from externalDocs
set @.selCount = @.@.ROWCOUNT
IF @.@.ROWCOUNT = 0
BEGIN
SET @.Msgs = 'No folders meet this selection criteria'
RETURN
END
else
BEGIN
SET @.Msgs = @.selCount
END
return
-------------------------
the asp code is
Dim cnnStoredProc ' Connection object
Dim cmdStoredProc ' Command object
Dim rstStoredProc ' Recordset object
Dim folderText
Set cnnStoredProc = Server.CreateObject("ADODB.Connection")
cnnStoredProc.Open db
' get the correct records for a page
Set cmdStoredProc = Server.CreateObject("ADODB.Command") ' Create Command object we'll use to execute the SP
cmdStoredProc.ActiveConnection = cnnStoredProc ' Set our Command to use our existing connection
cmdStoredProc.CommandText = "test" ' Set the SP's name and tell the Command object
cmdStoredProc.CommandType = adCmdStoredProc
cmdStoredProc.Parameters.Refresh
' ---- SET PARAMETERS --GET A PAGE WORTH OF RECORD---------------------
set prop=ADODB.Parameter
cmdStoredProc.Parameters("@.functionCode").Value = "L" 'functionCode
cmdStoredProc.Parameters("@.customerID").Value = customerId 'CustomerID
if searchCriteria <> "" then
cmdStoredProc.Parameters("@.searchcriteria").Value = searchCriteria 'search string
end if
if showAssigned = "Y" then
cmdStoredProc.Parameters("@.excludeDefined").Value = "Y" '
end if
set rstStoredProc = cmdStoredProc.Execute
'
response.write("msgs=" & cmdStoredProc("@.Msgs")) ' <<<<<<< THIS DOESN'T WORK------------------------------
I can enumerate through the recordset but the output parameter is blank.
Any Ideas
Thanks
i believe RecordSet have a property called RecordCount...
example:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("northwind.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
sql="SELECT * FROM Customers"
rs.Open sql,conn
if rs.Supports(adApproxPosition)=true then
i=rs.RecordCount
response.write("The number of records is: " & i)
end if
rs.Close
conn.Close
%>
Sunday, February 26, 2012
Getting Identity after Update method
I am adding records via
rs.AddNew
...
rs.Update
Is the new Identity value available immediately after the Update method, for
example
rs.Update
lngNewID = rs.RecordID
where RecordID is the Identity field. Thanks.
DavidDon't use AddNew, use an INSERT statement... or better yet, encapsulate it
in a stored procedure.
http://www.aspfaq.com/2174
http://www.aspfaq.com/2201
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"David C" <dlchase@.lifetimeinc.com> wrote in message
news:uElqca#KFHA.4092@.tk2msftngp13.phx.gbl...
> I have a table recordset using rs.Open .....
> I am adding records via
> rs.AddNew
> ...
> rs.Update
> Is the new Identity value available immediately after the Update method,
for
> example
> rs.Update
> lngNewID = rs.RecordID
> where RecordID is the Identity field. Thanks.
> David
>|||I agree and normally always use stored procs. However, I need to run
another function before I am done with this record that has to use the
new ID to populate one or more records in another table, return the
total and update the new record I am on.
David
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!|||> I agree and normally always use stored procs. However, I need to run
> another function before I am done with this record that has to use the
> new ID to populate one or more records in another table, return the
> total and update the new record I am on.
Why can't the stored procedure do this also? Or call another stored
procedure that does?
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.|||I don't understand why you can't do this with a proc but anyway you
should be able to retrieve the IDENTITY based on key value(s) for the
row you just added. IDENTITY should never be the only key of a table so
just use an alternate key:
SELECT id
FROM YourTable
WHERE key_col = <x>
Of course if you didn't declare an alternate key then you have other
problems too! :-)
David Portas
SQL Server MVP
--|||David,
You are correct, after update rs("ID_Col") will contain the newly inserted
id after Update is run.
-- Alex
"David C" wrote:
> I have a table recordset using rs.Open .....
> I am adding records via
> rs.AddNew
> ...
> rs.Update
> Is the new Identity value available immediately after the Update method, f
or
> example
> rs.Update
> lngNewID = rs.RecordID
> where RecordID is the Identity field. Thanks.
> David
>
>|||Mostly because the function that creates the related table records is
extremely complicated and conditional that it would take too long to
write and test it. This is an application that was ported from an
Access back end to SQL Server and I am under a time constraint so I have
to use as much existing code as possible. Hopefully, I'll have time to
analyze it later and maybe then I can convert to a stored proc(s), which
is my preference also.
p.s. The other function is a very complicated union dues calculation
that was set up for a government agency. Please try to understand.
Thanks
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!|||> Please try to understand.
I think I do understand. Reminds me of things like:
"Don't fix the leaking toilet. Just build a drain from your bathroom to the
reverse osmosis tank in the basement."|||That's a bit of a harsh analogy, don't you think ;-).
The ADO method has its advantages:
> quicker to develop & implement
> doesn't require create_proc permission in db (ie, doesn't involve getting
someone to grant those)
How about, "Wrap a bunch of ducttape around the pipe from the leaking
toilet" instead?
I'm sure it'll work just as well, but it isn't pretty and it isn't the right
way to do it ... but it's a heck of a lot cheaper.
-- Alex Papadimoulis
"Aaron [SQL Server MVP]" wrote:
> I think I do understand. Reminds me of things like:
> "Don't fix the leaking toilet. Just build a drain from your bathroom to t
he
> reverse osmosis tank in the basement."
>
>