Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jogin malathi
Posting Yak Master
117 Posts |
Posted - 2007-05-04 : 07:36:39
|
Hi alliam creating view as followsif exists drop view v1create view v1 asSelect Employee_id ,round((datediff(m,min(start_date),getdate()))/12.0,1) as Years from Sagarsoft.dbo.o_employeeexp (nolock) group by employee_id,start_datei want to check before creatuing view but i gives erros as followsIncorrect syntax near the keyword 'drop'.'CREATE VIEW' must be the first statement in a query batch.No rows affected.Malathi Rao |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-04 : 07:39:19
|
[code]if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[v1]') and OBJECTPROPERTY(id, N'IsView') = 1)drop view [dbo].[v1]GOcreate view dbo.v1 asSelect Employee_id ,round((datediff(m,min(start_date),getdate()))/12.0,1) as Years from Sagarsoft.dbo.o_employeeexp (nolock) group by employee_id,start_dateGO[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
jogin malathi
Posting Yak Master
117 Posts |
Posted - 2007-05-07 : 00:39:04
|
quote: Originally posted by harsh_athalye
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[v1]') and OBJECTPROPERTY(id, N'IsView') = 1)drop view [dbo].[v1]GOcreate view dbo.v1 asSelect Employee_id ,round((datediff(m,min(start_date),getdate()))/12.0,1) as Years from Sagarsoft.dbo.o_employeeexp (nolock) group by employee_id,start_dateGO Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
if exists (select * from dbo.sysobjects where id = object_id(N'dbo.v1') and OBJECTPROPERTY(id, N'IsView') = 1)drop view dbo.v1create view v1 asSelect Employee_id ,round((datediff(m,min(start_date),getdate()))/12.0,1) as Years from Sagarsoft.dbo.o_employeeexp (nolock) group by employee_id,start_dateIf iam saving the above query it shows the following errorIncorrect syntax near keyword "view"Malathi Rao |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-05-07 : 00:42:49
|
You need the keywords "GO" in between the DROP and the CREATE.Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-07 : 00:43:22
|
after drop voew u should tell the sql server that the batch is completed ...put Go after drop view |
|
|
jogin malathi
Posting Yak Master
117 Posts |
Posted - 2007-05-07 : 00:49:22
|
quote: Originally posted by pbguy after drop voew u should tell the sql server that the batch is completed ...put Go after drop view
Ya i tried with go after drop statement Again it shows the same error as incorrect syntax near "go"Malathi Rao |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-07 : 00:52:24
|
put go in the new line...do not put go along with drop view syntaxlike...if exists (select * from dbo.sysobjects where id = object_id(N'dbo.v1') and OBJECTPROPERTY(id, N'IsView') = 1)drop view dbo.v1 gocreate view v1 asSelect Employee_id ,round((datediff(m,min(start_date),getdate()))/12.0,1) as Years from Sagarsoft.dbo.o_employeeexp (nolock) group by employee_id,start_date |
|
|
jogin malathi
Posting Yak Master
117 Posts |
Posted - 2007-05-07 : 00:56:25
|
quote: Originally posted by pbguy put go in the new line...do not put go along with drop view syntaxlike...if exists (select * from dbo.sysobjects where id = object_id(N'dbo.v1') and OBJECTPROPERTY(id, N'IsView') = 1)drop view dbo.v1 gocreate view v1 asSelect Employee_id ,round((datediff(m,min(start_date),getdate()))/12.0,1) as Years from Sagarsoft.dbo.o_employeeexp (nolock) group by employee_id,start_date
ALTER PROCEDURE dbo.StoredProcedure2 AS if exists (select * from dbo.sysobjects where id = object_id(N'dbo.v1') and OBJECTPROPERTY(id, N'IsView') = 1)drop view dbo.v1go create view v1 asSelect Employee_id ,round((datediff(m,min(start_date),getdate()))/12.0,1) as Years from Sagarsoft.dbo.o_employeeexp (nolock) group by employee_id,start_dateYa i wrote the stored procs as above it shows the same errorAnd iam executing stored procs in SQL SERVER 2005 INTEGRATED WITH .NET IN VISUAL STUDIOMalathi Rao |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-07 : 06:00:54
|
As far I know, Go keyword can only be used when you are running the queries in the Query Analyser or SSMS, for any other enviorment it will not work, unless there is some special programming done for it. Chiraghttp://chirikworld.blogspot.com/ |
|
|
jogin malathi
Posting Yak Master
117 Posts |
Posted - 2007-05-07 : 06:17:02
|
quote: Originally posted by chiragkhabaria As far I know, Go keyword can only be used when you are running the queries in the Query Analyser or SSMS, for any other enviorment it will not work, unless there is some special programming done for it. Chiraghttp://chirikworld.blogspot.com/
ALTER PROCEDURE usp_r_search_internal @eid int ,@head_line varchar(150) ,@func_area varchar(150) ,@loc varchar(50) ,@exp varchar(50) ,@k_skills varchar(50) ,@emp varchar(50) ,@operator char(1) ASdelete from tempemployerdeclare @eno varchar(50) declare @edu1 varchar(150),@edu2 varchar(150),@education varchar(max),@skill varchar(150) ,@f_area varchar(max),@years varchar(50),@salary varchar(50),@location varchar(50)declare @employer varchar(150)declare dc1 cursor for select Employee_id from sagarsoft.dbo.o_employeebeginopen dc1fetch dc1 into @enowhile @@fetch_status=0beginset @employer=''set @education=''set @edu1=''set @edu2=''set @skill=''set @f_area=''set @salary = ''set @location ='' select @employer= case @employer when '' then employer else @employer + ','+ employer end from sagarsoft.dbo.o_employeeexp (nolock) where employee_id=@eno select @edu1= w.Education_Name from Sagarsoft.dbo.o_Education e (nolock) join Sagarsoft.dbo.O_EducationDetails w (nolock) on w.Education_Id=e.Education_Id where Education_End_Date=(select max(Education_End_Date) from Sagarsoft.dbo.o_Education (nolock) where employee_id=@eno) select @edu2= w.Education_Name from Sagarsoft.dbo.o_Education e (nolock) join Sagarsoft.dbo.O_EducationDetails w (nolock) on w.Education_Id=e.Education_Id where Education_End_Date=(select max(Education_End_Date) from Sagarsoft.dbo.o_Education (nolock) where Education_End_Date<(select max(Education_End_Date) from Sagarsoft.dbo.o_Education (nolock) where employee_id=@eno )and employee_id=@eno) select @Skill= case @skill when '' then n.skill_Name else @skill + ','+ n.skill_Name end, @f_area= case @f_area when '' then s.Functiional_Area else @f_area + ','+ s.Functiional_Area endfrom sagarsoft.dbo.o_skills s (nolock)left join Sagarsoft.dbo.o_skillsdetails n (nolock) on n.Skill_id=s.skill_idwhere employee_id=@eno Select @years=round((datediff(m,min(start_date),getdate()))/12.0,1) from Sagarsoft.dbo.o_employeeexp (nolock) where employee_id=@enoSelect @salary =Amount from sagarsoft.dbo.o_Currentjob (nolock) where employee_id=@enoSelect @location = Citytown from sagarsoft.dbo.o_Contacts (nolock) where employee_id=@eno if(@edu2='') begin set @education=@edu1 end else begin set @education= @edu1 +','+ @edu2 if(@education =',') begin set @education='' end end --print @edu insert into tempemployer values(@eno,@employer,@education,@Skill,@f_area,Round(@years,1),@salary,@location) fetch dc1 into @enoend close dc1 deallocate dc1 end ----creating view to store each employee resume detailsif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[v2]') and OBJECTPROPERTY(id, N'IsView') = 1)drop view [dbo].[v2]create view v2 as select 'Employer' as RecordType,id As Employee_Id,Employer,Salary,Functional_Area,Skills,Location, Years,Education,E.Employee_First_Name+' ' +E.Employee_Last_Name as Ename from RPO.dbo.tempemployer T Left join Sagarsoft.dbo.o_Employee E on E.Employee_id=T.idUnion allselect 'Functional_Area' as RecordType,id AS Employee_Id,Employer,Salary,Functional_Area,Skills,Location, Years,Education,E.Employee_First_Name+' ' +E.Employee_Last_Name as Ename from RPO.dbo. tempemployer T Left join Sagarsoft.dbo.o_Employee E on E.Employee_id=T.idunion allselect 'Skills' as RecordType,id AS Employee_Id,Employer,Salary,Functional_Area,Skills,Location, Years,Education,E.Employee_First_Name+' ' +E.Employee_Last_Name as Ename from RPO.dbo.tempemployer T Left join Sagarsoft.dbo.o_Employee E on E.Employee_id=T.idunion allselect 'Location' as RecordType,id AS Employee_Id,Employer,Salary,Functional_Area,Skills,Location, Years,Education,E.Employee_First_Name+' ' +E.Employee_Last_Name as Ename from RPO.dbo.tempemployer T Left join Sagarsoft.dbo.o_Employee E on E.Employee_id=T.id Union allselect 'Experience' as RecordType,id as Employee_id,Employer,Salary,Functional_Area,Skills,Location, Years,Education,E.Employee_First_Name+' ' +E.Employee_Last_Name as Ename from RPO.dbo.tempemployer T Left join Sagarsoft.dbo.o_Employee E on E.Employee_id=t.id ---end of the View if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[v1]') and OBJECTPROPERTY(id, N'IsView') = 1)drop view [dbo].[v1]create view v1 as select distinct Employee_Id, Isnull(Employer,' ' ) + isnull(Functional_Area,' ')+isnull(Skills,' ') + isnull(Location,' ') + isnull(Years,' ') as Keywords from v2 --------Stored proce to search internal resource using multi search---------if(@operator='A') BEGIN declare @keyword nvarchar(max) declare @sql nvarchar(1000) --declare @key nvarchar(max) if(@head_line !='') begin SELECT v2.Employee_Id,Employer,Salary,Education,Location From RPO.dbo.V2 (nolock) join RPO.dbo.V1 on V1.Employee_id=v2.employee_id where Keywords like '%'+@head_line+'%' end elseselect @sql= 'SELECT v2.Employee_Id,Employer,Salary,Education,Location From RPO.dbo.V2 (nolock) where 1=1'print @sql IF @func_area !='' begin SELECT @sql = @sql + ' and Functiional_Area like ''%''+'''+@func_area+'''+''%''' print'2' print @sql end IF @loc !='' begin SELECT @sql = @sql + ' and Location like ''%''+'''+@loc+'''+''%''' print'3' print @sqlendIF @exp !=''begin SELECT @sql = @sql + ' and Total_exp like ''%''+'''+@exp+'''+''%''' print'4' print @sqlend IF @k_skills !=''begin SELECT @sql = @sql + ' and Skills like ''%''+'''+@k_skills+'''+''%''' print'5' print @sqlendIF @emp !=''begin if @eid=1 begin SELECT @sql = @sql + ' and Employer like ''%''+'''+@emp+'''+''%'''--='''+@employer+'''' print'6' print @sql end else begin SELECT @sql = @sql + ' and Employer ='''+@emp+'''' print'66' print @sql endend exec sp_executesql @sql END If iam storing the above procs it shows errors as invalid syntax near 'view'Malathi Rao |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-07 : 06:24:40
|
You can't create objects like View/SP/function inside a Stored Proc (except using D-Sql), neither you should do it.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-07 : 06:25:32
|
There are 2 options1) Run each scripts sepeartely, like run the Alter of the Script and then run the Create View and so on and so forth. 2) Else at the run time, take the Consider Go as the seperater between two scripts and run the between script. Chiraghttp://chirikworld.blogspot.com/ |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-07 : 06:28:50
|
quote: Originally posted by harsh_athalye You can't create objects like View/SP/function inside a Stored Proc (except using D-Sql), neither you should do it.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
Dude, Is OP trying to create the script or trying to run the script from another enviorment??Chiraghttp://chirikworld.blogspot.com/ |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-07 : 07:07:07
|
OP wants to create view from inside stored procedure, which is not possible.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
jogin malathi
Posting Yak Master
117 Posts |
Posted - 2007-05-07 : 07:39:40
|
quote: Originally posted by harsh_athalye OP wants to create view from inside stored procedure, which is not possible.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
Thanks harsh i got the solution for my problemMalathi Rao |
|
|
sidpad
Starting Member
1 Post |
Posted - 2007-05-16 : 15:57:39
|
Hi Malathi Rao,Can you please how did you do it?Sid |
|
|
|
|
|
|
|