在sql中加入计数

时间:2011-12-05 04:33:25

标签: sql-server sql-server-2005

我的查询是:

CREATE PROC [dbo].[GetCount] 
@PID INT,
@IA BIT=null
AS
BEGIN
 SELECT count(*) from [dbo].[Ps] 
 WHERE  P.PID= @PID
 FROM Ps p inner join PMU pm ON p.PSID=pm.PSID 
 AND pm.PID=@PID and pm.IA=@IA 
END

但我得到了一个语法错误.Plz帮助找到我的错误。谢谢。

3 个答案:

答案 0 :(得分:0)

尝试:

CREATE PROC [dbo].[GetCount] 
@PID INT,
@IA BIT=null
AS
BEGIN
 SELECT count(*) 
 FROM Ps p 
      INNER JOIN PMU pm ON p.PSID=pm.PSID 
                           AND pm.PID=@PID 
                           AND pm.IA=@IA 
 WHERE  P.PID= @PID
END

答案 1 :(得分:0)

以下是sql的SELECT语句的格式:

SELECT <ColumnName1, ColumnName2...>
FROM <TableName1, TableName2...>
WHERE <Conditions...>
ORDER BY <ColumnName1...>

所以,如果我重新编写你的代码,如下所示:

CREATE PROC [dbo].[GetCount] 
@PID INT,
@IA BIT = null
AS
BEGIN
 SELECT COUNT(*)
 FROM Ps p INNER JOIN PMU pm ON p.PSID = pm.PSID 
          AND pm.PID=@PID AND pm.IA=@IA 
  WHERE  P.PID= @PID
END

答案 2 :(得分:0)

试试这个:[你也可以尝试重命名参数]

  

CREATE PROC [dbo]。[GetCount] @PID INT,@IA BIT = null AS BEGIN SELECT   从[dbo]计算(*)。[Ps] WHERE P.PID = @PID FROM Ps p inner join   PMU pm其中p.PSID = pm.PSID和pm.PID=@PID和pm.IA=@IA END