Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL


T-SQL

Author
Message
pksutha
pksutha
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: 58
And are you sure this below code executed successfully in 2005 ?

This is not a correct syntax in sqlserver2005 which is below given query
insert into #test values
('1','A'),
('','B')



correct answer:

insert into #test values('1','A')

insert into #test values ('','B')

this is the way to insert the values to the particular table in sqlserver2005
kannappanvrmk
kannappanvrmk
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 49
sorry ,i'm not getting .
i got first answer in sql server2008
tejaswini.patil
tejaswini.patil
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1060 Visits: 234
In SQL Server 2008 the INSERT statement has been enhanced which enables to insert multiple records using the VALUE clause in a single INSERT statement.

insert into #test values
(2,'D'),
('','E')
Above INSERT statement will insert two records.

In first set of values, 2 is not embedded in single quotes and that is why the data type is considered as "int". SQL Server considers that data type for the first value of all records being inserted in that INSERT statement.
First set of values inserts a record as 2, D
While inserting second record, the first value '' is implicitly converted as 0 so second set of values inserts a record as 0, E.


This question gave us a learning that we have to be careful about implicit conversion that SQL Server does.
Gopi S
Gopi S
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 325
Hi pksutha,

Let me explain little more..


insert into #test values ('1','A'), ('','B')

is not same as below.

insert into #test values('1','A')
insert into #test values ('','B')

The former insert statement in introduced in 2008 and the later is the usual code that we use often.

To be compatible in both, pls run the below code either in 2005 or 2008 and compare the results.

select '1' as c1,'A' as c2
union
select '','B'

Result1:
c1 c2
1 A
B

go

select 1 as c1,'A' as c2
union
select '','B'

Result2:
c1 c2
1 A
0 B


The important thing that i want to bring out is the datatype of the final result set is based on the first statement in the insert list.
From the above example ,
first result set has c1 [varchar(1)] , C2 [varchar(1)]
second result set has c1 [int, C2 [varchar(1)]

Hope this is helpful ?

Thanks
Gopi
pksutha
pksutha
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: 58
Hi Gopi,


i was explaing about insertion syntax,

not about selection process. In both 2005 and 2008 will display ans for selection command for insertion command.

bcoz sqlseerver 2005 doesn't support this syntax like insert into #test values('1','A'),(' ','B')

With Regards
Sutha
pksutha
pksutha
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: 58
For datatype , this doubt clarification i've posted one answered already.
varchar accepts both number and character and without single quote number also like (1,'b') or ('1','b')
both possible in sqlserver2005
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5802 Visits: 7137
Good question, thanks.
This serves as a gentle reminder of the dangers of relying on implicit conversion.

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
pksutha
pksutha
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: 58
ya i'm Agreeing this.

First of all insert into #test values(2,'A'),(' ' ,'B') this is not there in sqlserver 2005.

So now we can't consider data type issue.

IF u go with sqlserver2008 ,yes of course we need to consider about data types.

Thank you
Gopi S
Gopi S
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 325
Hi Sutha,
Still you have same issue in SQL Server 2005 using old method.
Try this and compare the results.

select * into #D1 from (
select '1' as c1,'A' as c2
union
select '','B'
) T

select * from #D1
go

select * into #D2 from (
select 1 as c1,'A' as c2
union
select '','B'
) T
select * from #D2

drop table #D1
drop table #D2

Implicit conversion matters both in 2005 and 2008 when using union in the select before inserting the records.
OCTom
OCTom
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2815 Visits: 4152
That new syntax in 2008 is handy. I'll have to try it out. Nice to learn something new. Thanks.
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