-6

我有这个要转换为存储过程的 sql 语句。我怎样才能做到这一点?

select a.name, a.lastname, from myTable where b.name= a.lastname

谢谢

更新这是对的人吗

CREATE PROCEDURE myProcedure
AS
BEGIN
SELECT [a.name],
       [a.lastname]
FROM [myTable]
WHERE [b.name] = a.lastname
END

这是我真正正在研究的代码之一

sql语句

SELECT RTRIM(l.Lab1) + '/' + RTRIM(l.LabR) AS Lab, meta.Title,ls.[ID],ls.[surveyID], ls.[specialistID],ls.[isCompleted] FROM [MyDIM].[dbo].[lsLabSurvey] ls JOIN [MYDIM].[dbo].[dimDocMetaInfo] meta ON meta.id = ls.[surveyID] and specialistID = @PID and isCompleted=1 JOIN MyDatabase.DBO.PID_Table l ON ls.ID = l.ID

和我尝试的存储过程

CREATE PROCEDURE PROCD1
AS
BEGIN
SELECT RTRIM(l.Lab1) + '/' + RTRIM(l.LabR) 
AS Lab, 
   meta.Title,
   la.[ID],
   ls.[surveyID],
   ls.[specialistID],
   ls.[isCompleted]
FROM [MyDIM].[dbo].[lsLabSurvey] ls 
JOIN [MyDIM].[dbo].[dimDocMetaInfo] meta 
ON meta.id = ls.[surveyID] 
and specialistID = @PID 
and isCompleted=1 
JOIN MyDatabase.DBO.PID_Table l 
ON ls.ID = l.ID 

结尾

4

2 回答 2

0

这真的没那么难。你已经完成了 95% 的工作。以下是针对 SQL Server 的:

CREATE PROCEDURE SomeProcedureName  
AS  
select a.name, a.lastname, from myTable where b.name= a.lastname

我强烈推荐这本书:http ://www.barnesandnoble.com/w/sams-teach-yourself-sql-in-10-minutes-ben-forta/1100070678

它涵盖了基本的 SQL 语法,并给出了 SQL 不同“方言”的示例。每个 RDBMS 执行类似功能的语法略有不同,您需要了解您正在使用的数据库的方言。

于 2012-09-27T20:02:50.080 回答
0

正如大卫所说,你快到了,但只需要稍作改动。

  1. 我想@Pid 是一个参数,如果是这样,存储过程定义中缺少它。
  2. (和specialistId=@Pid 和iscompleted =1)形成Where 子句(过滤器表达式)。所以程序是这样的

`

CREATE PROCEDURE PROCD1 (@PID INT )
AS 
    BEGIN
        SELECT  CONVERT(VARCHAR,RTRIM(l.Lab1)) + '/' + CONVERT(VARCHAR,RTRIM(l.LabR)) AS Lab ,
                meta.Title ,
                la.[ID] ,
                ls.[surveyID] ,
                ls.[specialistID] ,
                ls.[isCompleted]
        FROM    [MyDIM].[dbo].[lsLabSurvey] ls
                INNER JOIN [MyDIM].[dbo].[dimDocMetaInfo] meta ON meta.id = ls.[surveyID]
                INNER JOIN MyDatabase.DBO.PID_Table l ON ls.ID = l.ID
        WHERE   ls.specialistID = @PID
                AND ls.isCompleted = 1
    END 
`

希望这可以帮助。

于 2012-09-27T22:46:18.520 回答