July 12, 2013 at 7:30 am
--this is returning duplicate and while loop is not ending can u please help
create procedure [dbo].[testcms] as
SET NOCOUNT ON
Declare @description varchar(100)
Declare @groupname varchar(100)
Declare @server_type int
Declare @parent_id int
DECLARE @groupCount INT
declare @is_sytem_object int
set @description='test'
set @server_type=0
set @parent_id= 1
set @is_sytem_object=0
DECLARE @allgroup TABLE (
Name Varchar(50))
begin
with A as
(select Alertgroup as name,servername,connectionstring from Scomdata where alertgroup not in
(select name from msdb.dbo.sysmanagement_shared_server_groups_internal ))
insert into @allgroup
select distinct name
from A where name <>''
SELECT TOP 1 @groupname=Name
FROM @allGroup
ORDER BY Name
Set @groupCount = @@rowcount
while @groupcount >0
begin
insert into msdb.dbo.sysmanagement_shared_server_groups_internal
values(@groupname,@description,@server_type,@parent_id,@is_sytem_object)
end;
DELETE
FROM @allgroup
WHERE Name = @GroupName
SELECT TOP 1 @groupname=Name
FROM @allGroup
ORDER BY Name
Set @groupCount = @@rowcount
Return
end;
--this is returning duplicate and while loop is not ending can u please help
July 12, 2013 at 7:34 am
sorry guys ignore just missed one end 😛
July 12, 2013 at 8:23 am
Glad you got it sorted out but...I don't see why you need a loop for this at all.
I am pretty sure you could replace your entire script with a single insert statement.
insert into msdb.dbo.sysmanagement_shared_server_groups_internal
select distinct Alertgroup as name, 'test' as Description, 0 as server_type, 1 as parent_id, 0 as is_system_object
--, servername, connectionstring Don't use these other columns
from Scomdata where alertgroup not in
(
select name from msdb.dbo.sysmanagement_shared_server_groups_internal
)
where Alertgroup > ''
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply