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


DB Project Publish Empty string inserting a Zero


DB Project Publish Empty string inserting a Zero

Author
Message
maurociaccio
maurociaccio
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 59
Hi,
we have a Visual Studio 2013 DB Project which we use to create and populate a test database.

We have a table with a varchar NULL column (defaultvalue), and when we run the publish.sql script any INSERT of '' get converted to '0' values.

This is the table definition


CREATE TABLE [dbo].[CatalogueItemExtensionFields]
(
[RowID] tinyint identity not null,
[FieldType] tinyint not null,
[Description] varchar(120) not null,
[Nullable] bit not null,
[DefaultValue] varchar(100) null,
[Active_Flag] bit null,
[OrderPriority] tinyint not null,
[ContextGuid] uniqueidentifier not null
);



This is the INSERT statement, and we want to insert an empty string into the "defaultvalue" field


set identity_insert CatalogueItemExtensionFields on

INSERT INTO CatalogueItemExtensionFields (rowid, fieldtype, description, nullable, defaultvalue, active_flag, orderpriority)
VALUES (1, 3, 'Product Group', 0, '', 1, 1)

set identity_insert CatalogueItemExtensionFields off



If we run the above script manually it works fine. But when it is run in SQLCMD mode by the DB project PUBLISH action, we see that '0' is inserted.

What we have also done is modify the publish.sql file manually, as follows


set identity_insert CatalogueItemExtensionFields on

INSERT INTO CatalogueItemExtensionFields (rowid, fieldtype, description, nullable, defaultvalue, active_flag, orderpriority)
VALUES (1, 3, 'Product Group', 0, 'TEST STRING', 1, 1)

set identity_insert CatalogueItemExtensionFields off



When we manually execute the publish.sql we get this error:


'Msg 245, Level 16, State 1, Line 7 Conversion failed when converting the varchar value 'test' to data type int.'



So this means that the SQLCMD thinks the field is an INT, but that is not how it appears in the table definition.

Is there an obscure DB setting used in SQLCMD mode?

Thanks for your help
maurociaccio
maurociaccio
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 59
Hi,

sorry but I had missed a crucial point. I was running multiple INSERT statements, and in some of those I was writing an INT to the varchar field.

As a result, SQL Server "decided" that the field was really (?) an INT, and giving me the error when I tried to insert a string.

Not sure I understand why though.

set identity_insert CatalogueItemExtensionFields on

INSERT INTO CatalogueItemExtensionFields (rowid, fieldtype, description, nullable, defaultvalue, active_flag, orderpriority) VALUES
(6, 3, N'Product Group', 0, N'', 1, 6),
(7, 2, N'Minimum Order Quantity', 1, NULL, 1, 7),
(8, 3, N'Additional HIBCs', 0, 1, 1, 8)
set identity_insert CatalogueItemExtensionFields off

If I instead run the following statement, with a default value of '1' for RowID = 8, it all works fine.

INSERT INTO CatalogueItemExtensionFields (rowid, fieldtype, description, nullable, defaultvalue, active_flag, orderpriority) VALUES
(6, 3, N'Product Group', 0, N'', 1, 6),
(7, 2, N'Minimum Order Quantity', 1, NULL, 1, 7),
(8, 3, N'Additional HIBCs', 0, '1', 1, 8),

So, why does SQL Server do this?

Full post on Stack Overflow
Lowell
Lowell
SSC Guru
SSC Guru (286K reputation)SSC Guru (286K reputation)SSC Guru (286K reputation)SSC Guru (286K reputation)SSC Guru (286K reputation)SSC Guru (286K reputation)SSC Guru (286K reputation)SSC Guru (286K reputation)

Group: General Forum Members
Points: 286747 Visits: 41913
empty string converts to an int without an error, and it's value is zero. it has to do with the precidence of implicit conversions

you could use the NULLIF function to resolve this, but i'm not sure if that's possible in your case or not


INSERT INTO CatalogueItemExtensionFields (rowid, fieldtype, description, nullable, defaultvalue, active_flag, orderpriority)
SELECT 1, 3, 'Product Group', 0,NULLIF('',''), 1, 1 UNION ALL
SELECT 2, 3, 'Product Group', 0,NULLIF('4',''), 1, 1



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
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