I want to create view inside my function and whenever I would like to create my view I just refer to function and call it.
I wrote this code but I got an error:
create FUNCTION [dbo].[testFunc]()
RETURNS bigint
AS
BEGIN
IF OBJECT_ID ('dbo.r_Sales01_Requests__Duplicates', 'V') IS NOT NULL
DROP VIEW dbo.r_Sales01_Requests__Duplicates ;
go
create view r_Sales01_Requests__Duplicates (
CompanyID
,Branch
,Year
,VoucherType,VoucherNumber
,Date_Persian
,Row) as
select
CompanyID
,Branch
,Year
,VoucherType
,VoucherNumber
,Date_Persian
,Row
from t_SalesRequests
group by CompanyID, Branch, Year, VoucherType, VoucherNumber, Date_Persian, Row
having count(*)>1
return
END
Note : The below part is very important for me to have it while creating function .
IF OBJECT_ID ('dbo.r_Sales01_Requests__Duplicates', 'V') IS NOT NULL
DROP VIEW dbo.r_Sales01_Requests__Duplicates ;