1. 程式人生 > >儲存過程————執行拼接的SQL語句的字串

儲存過程————執行拼接的SQL語句的字串

 declare @tableName nvarchar(50)
set @tableName=case
     when @IDType='CustomerID' then 'Customer'
     when @IDType='EventRegistrationID' then 'EventRegistration'
     when @IDType='CustomerServiceID' then 'CustomerService'
     when @IDType='ReservationID' then 'Reservation'
     when @IDType='PartnerID' then 'Partner'
     when @IDType='LocationID' then 'Location'
     else 'LocationService'
end

declare @sqlstr nvarchar(500)

exec sp_executesql @sqlstr,N'@Count int output',@Count output

如果有引數的話,在執行這個字串時,將引數加在後面,例如:@Count