用TADOQuery如何查詢數據庫中的存儲過程所需參數(100分)

  • 主题发起人 主题发起人 zhuthesea
  • 开始时间 开始时间
Z

zhuthesea

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟在寫一個解釋器,為了執行存儲過程,想用TADOQuery如何查詢數據庫中的存儲過程所需參數,便於傳值,如果做呢?
 
直接使用控件就可以了。
 
首先,如果你的存储过程是Oracle的,那么建议不要放在包(Package)里面,否则调用格式必须用带花括号{}括起来,非常麻烦!<br>通过ADOQuery调用存储过程可以这么调:<br>&nbsp; with ADOQuery1 do<br>&nbsp; begin<br>&nbsp; &nbsp; Parameters.Clear;<br>&nbsp; &nbsp; Parameters.ParamByName('Param1').Value:= 'Value1';<br>&nbsp; &nbsp; Prepared:= true;<br>&nbsp; &nbsp; Open; &nbsp;<br>&nbsp; end;
 
我知道可以通過SQL查詢數據庫表的信息一些字段名和數據類型大小等等,但現在想查詢數據庫中存儲過程中的信息,有辦法嗎,必須這樣做,
 
MS-SQL Server<br>select specific_catalog 库名,<br>&nbsp; &nbsp; &nbsp; &nbsp;specific_schema &nbsp;用户名,<br>&nbsp; &nbsp; &nbsp; &nbsp;specific_name &nbsp; &nbsp;名称,<br>&nbsp; &nbsp; &nbsp; &nbsp;parameter_name &nbsp; 参数名,<br>&nbsp; &nbsp; &nbsp; &nbsp;data_type &nbsp; &nbsp; &nbsp; &nbsp;类型,<br>&nbsp; &nbsp; &nbsp; &nbsp;ordinal_position 位置<br>&nbsp; from INFORMATION_SCHEMA.PARAMETERS<br>&nbsp;where specific_name = 存储过程名<br>Oracle <br>SELECT * FROM USER_ARGUMENTS
 
接受答案了.
 
Schema: How do I show the parameters for a function or stored procedure?<br>Let's say you have the following procedure: <br>&nbsp;<br>CREATE PROCEDURE dbo.fakeProcedure <br>&nbsp; &nbsp; @foo INT = 5, <br>&nbsp; &nbsp; @bar DATETIME = GETDATE, <br>&nbsp; &nbsp; @dec DECIMAL(19,2), <br>&nbsp; &nbsp; @x TEXT, <br>&nbsp; &nbsp; @vc VARCHAR(255) OUTPUT <br>AS <br>BEGIN <br>&nbsp; &nbsp; DECLARE @ret INT <br>&nbsp; &nbsp; IF @foo &gt; 10 <br>&nbsp; &nbsp; &nbsp; &nbsp; SET @ret = 10 - @foo <br>&nbsp; &nbsp; ELSE <br>&nbsp; &nbsp; &nbsp; &nbsp; SET @ret = 10 + @foo <br>&nbsp; &nbsp; RETURN @ret <br>END <br>GO <br>&nbsp;<br>Now, you're planning on calling this stored procedure in your ASP code or in an application, and you forget the parameter names and/or their datatypes. <br>&nbsp;<br>Well, the first thing you could do is look at the second resultset of sp_help: <br>&nbsp;<br>EXEC sp_help 'fakeProcedure' <br>&nbsp;<br>However, this resultset does not differentiate between INPUT / OUTPUT parameters, nor does it show default values. <br>&nbsp;<br>There are a couple of built-in stored procedures that go into much greater detail about each parameter: <br>&nbsp;<br>EXEC sp_sproc_columns 'fakeProcedure' <br>&nbsp;<br>--or the undocumented/unsupported: <br>&nbsp;<br>EXEC sp_procedure_params_rowset 'fakeProcedure' <br>&nbsp;<br>These give far more information than necessary, and still don't show default values for applicable params. <br>&nbsp;<br>You can also look at the syscolumns table: <br>&nbsp;<br>SELECT * FROM syscolumns <br>&nbsp; &nbsp; WHERE id = OBJECT_ID('fakeProcedure') <br>&nbsp;<br>Again, default values are not returned. <br>&nbsp;<br>Finally, you could create your own procedure using the PARAMETERS view under INFORMATION_SCHEMA, to give you a much more concise result: <br>&nbsp;<br>CREATE PROCEDURE dbo.listProcParams <br>&nbsp; &nbsp; @proc_name SYSNAME <br>AS <br>BEGIN <br>&nbsp; &nbsp; SELECT &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; [Param] = COALESCE(PARAMETER_NAME, '&lt;no params&gt;'), <br>&nbsp; &nbsp; &nbsp; &nbsp; [DataType] = COALESCE(UPPER(DATA_TYPE) + CASE <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN DATA_TYPE IN ('NUMERIC', 'DECIMAL') THEN &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '(' + CAST(NUMERIC_PRECISION AS VARCHAR) &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + ', ' + CAST(NUMERIC_SCALE AS VARCHAR) + ')' &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN RIGHT(DATA_TYPE, 4) = 'CHAR' THEN <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + ')' <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE '' END + CASE PARAMETER_MODE &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN 'INOUT' THEN ' OUTPUT' ELSE ' ' END, '-') <br>&nbsp; &nbsp; FROM &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; INFORMATION_SCHEMA.PARAMETERS <br>&nbsp; &nbsp; WHERE <br>&nbsp; &nbsp; &nbsp; &nbsp; SPECIFIC_NAME = @proc_name <br>&nbsp; &nbsp; ORDER BY &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; ORDINAL_POSITION <br>END <br>GO <br>&nbsp;<br>EXEC dbo.listProcParams 'fakeProcedure' <br>&nbsp;<br>Result: <br>&nbsp;<br>Param &nbsp;DataType <br>------ ---------------------- <br>@foo &nbsp; INT &nbsp;<br>@bar &nbsp; DATETIME &nbsp;<br>@dec &nbsp; DECIMAL(19, 2) &nbsp;<br>@x &nbsp; &nbsp; TEXT &nbsp;<br>@vc &nbsp; &nbsp;VARCHAR(255) OUTPUT <br>&nbsp;<br>Now, you'll notice if you run the following... <br>&nbsp;<br>SELECT * <br>&nbsp; &nbsp; FROM INFORMATION_SCHEMA.PARAMETERS <br>&nbsp; &nbsp; WHERE SPECIFIC_NAME='fakeProcedure' <br>&nbsp;<br>...that you *still* can't determine the default values. The sad truth is that these are simply not stored by SQL Server, they are merely evaluated by the text of the procedure at runtime. So, to determine default values, you're going to have to visually inspect the actual SP code, using sp_helptext or the ROUTINES view under INFORMATION_SCHEMA: <br>&nbsp;<br>EXEC sp_helptext 'fakeProcedure' <br>&nbsp;<br>--or <br>&nbsp;<br>SELECT ROUTINE_DEFINITION <br>&nbsp; &nbsp; FROM INFORMATION_SCHEMA.ROUTINES <br>&nbsp; &nbsp; WHERE ROUTINE_NAME = 'fakeProcedure' <br>&nbsp;<br>Please let us know if you have any tricks for determining default values programmatically (well, aside from brute force parsing, which is probably more work than resigning yourself to just look up the code for the rest of your life &lt;G&gt;).<br>Related Articles<br>&nbsp;Schema: How do I list all the indexes in a database?<br>&nbsp;Schema: How do I list the databases on my server?<br>&nbsp;Schema: How do I show all the primary keys in a database?<br>&nbsp;Schema: How do I show all the triggers in a database?<br>&nbsp;Schema: How do I show the columns for a table?<br>&nbsp;Schema: How do I show the description property of a column?<br>&nbsp;Schema: How do I show the stored procedures in a database?<br>&nbsp;Schema: How do I show the tables in a database?<br>&nbsp;Schema: How do I show the user-defined functions (UDFs) in a database?<br>&nbsp;Schema: How do I show the views in a SQL Server database?
 
后退
顶部