用于检查现有值的存储过程

时间:2016-07-01 03:19:37

标签: sql-server stored-procedures

表项目有5个项目ID。存储过程的参数为@projectid。如果用户使用参数6执行存储过程,则应该返回'项目6不存在'。

projectid
---------
 1
 2
 3
 4
 5

这就是我正在尝试的

create procedure chk_project @projectid int 
as begin
if 
@projectid!=(select projectid from project where projectid=@projectid)
print @projectid+'does not exist

4 个答案:

答案 0 :(得分:2)

我认为它可以帮到你

  create procedure Usp_project
  (
      @projectid int
  )
  As
  /*
  Exec Usp_project 6
  */
  if not exists(select projectid from project where projectid = @projectid)
  begin
      print('Projectid does not Exist')
  end

答案 1 :(得分:0)

您可以这样做:

<?php

class GuestBook
{
    private static $numInstances = 0;
    private static $var1 = 10;

    function __construct()
    {
        static::$numInstances++;
    }

    public static function getNumInstances()
    {
        return GuestBook::$numInstances;
    }

    public function getVar1()
    {
        return GuestBook::var1;
    }
}

$instance1 = new GuestBook();
$instance2 = new GuestBook();

echo(GuestBook::getNumInstances());

?>

答案 2 :(得分:0)

试试这个:

Create procedure chk_project
@projectid int 
as
begin
    if not exists (select 1 from project where projectid = @projectid)
    begin
        print cast(@projectid as varchar) + ' does not exist';
    end
end

答案 3 :(得分:0)

尝试这样,此SP将输出结果作为结果集

CREATE PROCEDURE chk_project (@projectid INT)
AS
BEGIN
    IF NOT EXISTS (
            SELECT 1
            FROM project
            WHERE projectid = @projectid
            )
        SELECT cast(@projectid AS VARCHAR) + ' does not exist' as Status;
    ELSE
        SELECT cast(@projectid AS VARCHAR) + '  exist' as Status;
END;