English 中文(简体)
如何使用存储过程在select语句中填充值?
原标题:How can I populate a value in a select statement using a stored procedure?

前驱:我必须使用存储过程,而且我使用的是SQL Server 2000。我还需要吐出一张表,以便Reporting Services能够正确使用它。但是,我可以创建任何数据库对象来帮助获取数据。

所以,这是我的select语句的一个例子(我希望我让它看起来和我实际使用的一样)

SELECT     
    Store.Store_ID, 
    Store.Store_Description, 
    Salaries.Income, 
    Salaries.Tax_Rate, 
    Managers.Manager_Name, 
    Employees.Employee_ID

FROM         
    Store INNER JOIN
    Managers ON Managers.Store_ID = Store.Store_ID INNER JOIN
    Employees ON Managers.Manager_ID = Employees.Manager_ID INNER JOIN
    Salaries ON Employees.Salary_Type_ID = Salaries.Salary_Type_ID 
WHERE (Store.Store_ID = 20561) 
ORDER BY Employee_ID

编辑:由于该过程返回一个值,我只想这样做:

SELECT     
    Store.Store_ID, 
    Store.Store_Description, 
    Salaries.Income, 
    Salaries.Tax_Rate, 
    Managers.Manager_Name, 
    Employees.Employee_ID,
    Exec proc_Get_Emplyee_estimation_Costs Employee_ID    
FROM         
    Store INNER JOIN
    Managers ON Managers.Store_ID = Store.Store_ID INNER JOIN
    Employees ON Managers.Manager_ID = Employees.Manager_ID INNER JOIN
    Salaries ON Employees.Salary_Type_ID = Salaries.Salary_Type_ID 
WHERE (Store.Store_ID = 20561) 
ORDER BY Employee_ID

我知道我显然不能这样做,但我需要能够使用员工编号在每一行上使用该程序。我不知道该怎么办。

我可以创建一个函数,在该函数中使用过程中select语句的结果,然后将其附加到表上并将表返回给Reporting Services吗

问题回答

我建议使用a用户定义函数,而不是存储过程。您可以在select语句中使用UDF来计算并返回所需的值,如下所示:

SELECT     
    Store.Store_ID, 
    Store.Store_Description, 
    Salaries.Income, 
    Salaries.Tax_Rate, 
    Managers.Manager_Name, 
    Employees.Employee_ID,
    udf_Get_Emplyee_estimation_Costs(Employee_ID) AS EstimationCost
FROM         
    Store INNER JOIN
    Managers ON Managers.Store_ID = Store.Store_ID INNER JOIN
    Employees ON Managers.Manager_ID = Employees.Manager_ID INNER JOIN
    Salaries ON Employees.Salary_Type_ID = Salaries.Salary_Type_ID 
WHERE (Store.Store_ID = 20561) 
ORDER BY Employee_ID

用户自定义功能:

CREATE FUNCTION udf_Get_Emplyee_estimation_Costs
(
    Employee_ID INT
)
RETURNS DOUBLE 
AS
RETURN 
(
    SELECT EstimationVal FROM SomeTable
)

why not use a function to return a value you can use the return value in the select statement

try http://msdn.microsoft.com/en-us/library/aa258261(v=sql.80).aspx





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...