SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


If else issue


If else issue

Author
Message
ashwinboinala
ashwinboinala
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 497
Declare @Version int;




select @version=(SUBSTRING(CONVERT(VARCHAR(50),SERVERPROPERTY('productversion')), 1,2))


if (@version >=11)

begin
IF OBJECT_ID('tempdb..#checkresults') IS NOT NULL

DROP TABLE #checkresults


create table #checkresults
(

[Error] [int] NULL,
[Level] [int] NULL,
[State] [int] NULL,
[MessageText] [varchar](650) NULL,
[RepairLevel] [varchar](150) NULL,
[Status] [int] NULL,
[DbId] [int] NULL,
[DbFragId] [int] NULL,
[ObjectId] [int] NULL,
[IndexId] [int] NULL,
[PartitionId] [int] NULL,
[AllocUnitId] [int] NULL,
[RidDbId] [int] NULL,
[RidPruId] [int] NULL,
[File] [int] NULL,
[Page] [int] NULL,
[Slot] [int] NULL,
[RefDbId] [int] NULL,
[RefPruId] [int] NULL,
[RefFile] [int] NULL,
[RefPage] [int] NULL,
[RefSlot] [int] NULL,
[Allocation] [int] NULL )

end


else



begin


IF OBJECT_ID('tempdb..#checkresults') IS NOT NULL

DROP TABLE #checkresults

create table #checkresults
(

[Error] [int] NULL,
[Level] [int] NULL,
[State] [int] NULL,
[MessageText] [varchar](650) NULL,
[RepairLevel] [varchar](150) NULL,
[Status] [int] NULL,
[DbId] [int] NULL,
--[DbFragId] [int] NULL,
[ObjectId] [int] NULL,
[IndexId] [int] NULL,
[PartitionId] [int] NULL,
[AllocUnitId] [int] NULL,
--[RidDbId] [int] NULL,
--[RidPruId] [int] NULL,
[File] [int] NULL,
[Page] [int] NULL,
[Slot] [int] NULL,
--[RefDbId] [int] NULL,
--[RefPruId] [int] NULL,
[RefFile] [int] NULL,
[RefPage] [int] NULL,
[RefSlot] [int] NULL,
[Allocation] [int] NULL )

end


select * from #checkresults


Msg 2714, Level 16, State 1, Line 59
There is already an object named '#checkresults' in the database.


this is the error i am getting can u please help me
parulprabu
parulprabu
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 136
SQL parses your script and finds that you are attempting to create it in two different places, and so raises an error, its better to create the table in the beginning with common columns and add the specific columns in the if--else based on the condition


Declare @Version int;




select @version=(SUBSTRING(CONVERT(VARCHAR(50),SERVERPROPERTY('productversion')), 1,2))



IF OBJECT_ID('tempdb..#checkresults') IS NOT NULL

DROP TABLE #checkresults

create table #checkresults
(

[Error] [int] NULL,
[Level] [int] NULL,
[State] [int] NULL,
[MessageText] [varchar](650) NULL,
[RepairLevel] [varchar](150) NULL,
[Status] [int] NULL,
[DbId] [int] NULL,
--[DbFragId] [int] NULL,
[ObjectId] [int] NULL,
[IndexId] [int] NULL,
[PartitionId] [int] NULL,
[AllocUnitId] [int] NULL,
--[RidDbId] [int] NULL,
--[RidPruId] [int] NULL,
[File] [int] NULL,
[Page] [int] NULL,
[Slot] [int] NULL,
--[RefDbId] [int] NULL,
--[RefPruId] [int] NULL,
[RefFile] [int] NULL,
[RefPage] [int] NULL,
[RefSlot] [int] NULL,
[Allocation] [int] NULL )
if (@version >=11)

begin
alter table #checkresults add
[DbFragId] [int] NULL,
[RidDbId] [int] NULL,
[RidPruId] [int] NULL,
[RefDbId] [int] NULL,
[RefPruId] [int] NULL

end


drop table #checkresults


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search