SQL Server:存储过程中的动态查询

时间:2013-11-09 10:43:38

标签: sql sql-server stored-procedures

我尝试在SQL Server中编写动态查询。但是我收到如下警告,

  

Selected存储过程或函数不返回任何列

我一直在努力实现这一点,但它仍然无法运行。

我在哪里错过了?

ALTER PROCEDURE [dbo].[S_ProjeGetir1]
@ID          int=0,
@AktifMi     int,
@ProjeFirma  int,
@Icinde      int,
@AramaText   Varchar(500),
@Arasinda1   Varchar(50),
@Arasinda2   Varchar(50)
AS
BEGIN 
 Create Table #TempTable 
(Sonuc int   
,ID int           
,FirmaID  int
,PrismProjeID  int
,ProjeAdi  nvarchar(150)
,RgID  uniqueidentifier
,HaritaEnlem  nvarchar(25)
,HaritaBoylam  nvarchar(25)
,ProjeTeslimTarihi  datetime
,HemenTeslimMi  bit
,Adres  nvarchar(250)
,AdresUlkeID  int
,AdresILID  int
,AdresILceID  int
,AdresSemtID  int
,KonutSayisi  int
,LansmanTarihi  datetime
,OfisSayisi  int
,MagazaSayisi  int
,BlokSayisi  int
,YesilAlan  int
,KrediyeUygunMu  bit
,AktifMi  bit
,UcBoyutVarMi  bit
,UlkeAdi  nvarchar(150)
,Sehir  nvarchar(50)
,Ilce  nvarchar(50)
,Semt  nvarchar(50)
,FirmaAdi  nvarchar(100)
,StokSonGuncellemeTarihi  datetime)

   Declare @SqlText varchar(8000),
           @AktifPasif bit
   Set @AktifPasif=Case When @AktifMi=0 Then 1  When @AktifMi=1 Then 0 End
   SET NOCOUNT ON
   BEGIN TRY

        Set @SqlText='  SELECT 1 Sonuc ,[ID],[FirmaID] ,[PrismProjeID],[ProjeAdi]    ,[RgID]      ,[HaritaEnlem] ,[HaritaBoylam]  ,[ProjeTeslimTarihi]
                                       ,[HemenTeslimMi],[Adres]       ,[AdresUlkeID] ,[AdresILID] ,[AdresILceID] ,[AdresSemtID]   ,[KonutSayisi] 
                                       ,[LansmanTarihi],[OfisSayisi]  ,[MagazaSayisi],[BlokSayisi],[YesilAlan]   ,[KrediyeUygunMu],[AktifMi]  
                                       ,[UcBoyutVarMi] ,[UlkeAdi]     ,[Sehir]       ,[Ilce]      ,[Semt]
                                       ,[FirmaAdi]     ,[StokSonGuncellemeTarihi] 
                        FROM [dbo].[V_Proje] AS P WITH (NOLOCK) 
                        WHERE 1=1 '
        If @ID>0 Set --ID ye Göre İlgili Kayıdı almak için
           @SqlText=@SqlText +' and ID=' +cast(@ID as Varchar(50))
        Else
        Begin
            --Aktif ise veya Pasif ise            
            if @AktifMi=0 or @AktifMi=1
            Begin
              Set @SqlText=@SqlText +' and AktifMi='+cast(@AktifPasif as varchar(50))
            End
            --Bütün Alanları sorgulamak için
              Set @SqlText=@SqlText +' and CASE WHEN '+cast(@ProjeFirma as varchar(50))+'=1 THEN Upper(ProjeAdi)
                                                WHEN '+cast(@ProjeFirma as varchar(50))+'=0 THEN Upper(FirmaAdi)
                                                WHEN '+cast(@ProjeFirma as varchar(50))+'=2 THEN Upper(HaritaEnlem)
                                                WHEN '+cast(@ProjeFirma as varchar(50))+'=3 THEN Upper(HaritaBoylam)
                                                WHEN '+cast(@ProjeFirma as varchar(50))+'=4 THEN Upper(Adres)'  

            If @Icinde=0--İçinde                                              
              Set @SqlText=@SqlText +' END Like ''%'+@AramaText+'%''  '
            If @Icinde=1--İle Başlayan
              Set @SqlText=@SqlText +' END Like '''+@AramaText+'%''  '
            If @Icinde=2--Arasında
              Set @SqlText=@SqlText +' END between '''+ @Arasinda1+''' and '''+@Arasinda2+''''
            --select @SQLTEXT
        End
        exec('insert into #TempTable ' + @SqlText)



        Select     Sonuc, [ID],[FirmaID],[PrismProjeID],[ProjeAdi] ,[RgID]          ,[HaritaEnlem],[HaritaBoylam],[ProjeTeslimTarihi],[HemenTeslimMi]
                        ,[Adres]       ,[AdresUlkeID] ,[AdresILID],[AdresILceID]   ,[AdresSemtID],[KonutSayisi] ,[LansmanTarihi]    ,[OfisSayisi]
                        ,[MagazaSayisi],[BlokSayisi]  ,[YesilAlan],[KrediyeUygunMu],[AktifMi]    ,[UcBoyutVarMi],[UlkeAdi],[Sehir]  ,[Ilce],[Semt]
                        ,[FirmaAdi]    ,[StokSonGuncellemeTarihi] 
        From #TempTable AS T  WITH (NOLOCK) 

   END TRY
   BEGIN CATCH
       SELECT -1 AS Sonuc -- EXCEPTION
   END CATCH
   SET NOCOUNT OFF
END

任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

我不确定我要说的是什么,它将用于EF,但我在LINQ to SQL类中有类似的情况。

  1. 备份存储过程的代码
  2. 在您的存储过程中创建一个具有适当值的SELECT语句,如下所示:

     ALTER PROCEDURE [dbo].[S_ProjeGetir1]
         @ID          int=0,
         @AktifMi     int,
         @ProjeFirma  int,
         @Icinde      int,
         @AramaText   Varchar(500),
         @Arasinda1   Varchar(50),
         @Arasinda2   Varchar(50)
     AS
     BEGIN
    
         Select CONVERT(VARCHAR(30),'') AS Sonuc, -- Make sure to convert to what your 
                0 AS [ID],
                0 AS [FirmaID],
                0 AS [PrismProjeID],
                ...
     END
    
  3. 在EF中导入此存储过程

  4. 如果一切顺利,请在存储过程中恢复备份的代码。