Friday, March 23, 2012
Interesting SQL Server behaviour
The following code describes a problem I'm having when I'm selecting
data via a Case statement.
The data I'm selecting is from a table with 2 varchar columns and 1
int column. When I select this data I get the following error:
Server: Msg245, Level 16,State 1, Line 1
Syntax error converting the varchar values 'A' to a column of datatype
int.
If I cast the int to a varchar it works, but I would prefer not to
cast if possible. SQL server seems to look at all the datatypes in the
case statement and if a int data type appears, it seems to be trying
to insert into a int column. Is this some sort of temp table? Anyone
know why it assigned an int column here?
Any ideas what I can do
Thanks,
Tom
- --
- -- Set up the table
- --
create table Tom
(charcol1 varchar(2)
,charcol2 varchar(2)
,intcol1 int
)
insert into tom
(charcol1
,intcol1
,charcol2
)
values
('A',1,'C')
- ---
- -- Code
- ---
SELECT
CASE numlist.list
WHEN 1 THEN
Tom.charcol1
WHEN 2 THEN
--cast (Tom.intcol1 as varchar(5))
Tom.intcol1
WHEN 3 THEN
Tom.charcol2
END AS result
FROM Tom
CROSS JOIN (select 1 as list
union all
select 2 as list
union all
select 3 as list
) as numlistThis is how CASE work, result from CASE expression is of the same datatype.
Output is determined as
per "datatype precedence" documented in Books Online.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Staggly" <tomstagg@.gmail.com> wrote in message
news:e646e15f.0502090622.8a26408@.posting.google.com...
> Hi,
> The following code describes a problem I'm having when I'm selecting
> data via a Case statement.
> The data I'm selecting is from a table with 2 varchar columns and 1
> int column. When I select this data I get the following error:
> Server: Msg245, Level 16,State 1, Line 1
> Syntax error converting the varchar values 'A' to a column of datatype
> int.
> If I cast the int to a varchar it works, but I would prefer not to
> cast if possible. SQL server seems to look at all the datatypes in the
> case statement and if a int data type appears, it seems to be trying
> to insert into a int column. Is this some sort of temp table? Anyone
> know why it assigned an int column here?
> Any ideas what I can do
> Thanks,
> Tom
> - --
> - -- Set up the table
> - --
> create table Tom
> (charcol1 varchar(2)
> ,charcol2 varchar(2)
> ,intcol1 int
> )
> insert into tom
> (charcol1
> ,intcol1
> ,charcol2
> )
> values
> ('A',1,'C')
> - ---
> - -- Code
> - ---
> SELECT
> CASE numlist.list
> WHEN 1 THEN
> Tom.charcol1
> WHEN 2 THEN
> --cast (Tom.intcol1 as varchar(5))
> Tom.intcol1
> WHEN 3 THEN
> Tom.charcol2
> END AS result
> FROM Tom
> CROSS JOIN (select 1 as list
> union all
> select 2 as list
> union all
> select 3 as list
> ) as numlist
Interesting SQL code editor
Fast SQL Editor
A new and powerfull SQL editor for the professional user.
www.activefrog.com
We are still looking for Beta Testers of this new product. Please visit the
web site and download this amazing tool.
The activeFrog.com team
"activefrog.com" <mail@.activefrog.com> schrieb im Newsbeitrag
news:%23YStFAwIFHA.1096@.tk2msftngp13.phx.gbl...
> Please check out:
> Fast SQL Editor
> A new and powerfull SQL editor for the professional user.
> www.activefrog.com
> We are still looking for Beta Testers of this new product. Please visit
the
> web site and download this amazing tool.
> The activeFrog.com team
Although this question should be forbidden in this group... :-) Do you
plan to go cross platform, i.e. support other SQL dialects as well?
Kind regards
robert
|||One question I have is why develop a new sql editor considering that the
market is flooded anyway?
activefrog.com wrote:
> Please check out:
> Fast SQL Editor
> A new and powerfull SQL editor for the professional user.
> www.activefrog.com
> We are still looking for Beta Testers of this new product. Please visit the
> web site and download this amazing tool.
> The activeFrog.com team
>
>
Wednesday, March 21, 2012
Interesting Query Results
below. Bad style/practice aside, I was surprised that it ran without
error and did not give "expected" results. Expected meaning five
one-row ouptuts. This is using SQL Server 2000, can anyone tell me if
2005 will act the same or different?
Thanks
declare @.int int
set @.int =0
while @.int<5
begin
declare @.tab table (policy varchar(12), mynum int)
declare @.i int
set @.i=isnull(@.i,0)+1
insert into @.tab
values('test',@.int)
select @.i as I,* from @.tab
set @.int=@.int+1
ENDThe declarations for the table and int variables get "moved" outside the loo
p
by the compiler. I agree the output is not what I would expect either, but I
also think the output is better than what I would expect - i.e. SQL Server i
s
very "smart".
"dott@.accessGeneral.com" wrote:
> I was doing a code review and found code that was similar to the code
> below. Bad style/practice aside, I was surprised that it ran without
> error and did not give "expected" results. Expected meaning five
> one-row ouptuts. This is using SQL Server 2000, can anyone tell me if
> 2005 will act the same or different?
> Thanks
> declare @.int int
> set @.int =0
> while @.int<5
> begin
> declare @.tab table (policy varchar(12), mynum int)
> declare @.i int
> set @.i=isnull(@.i,0)+1
> insert into @.tab
> values('test',@.int)
> select @.i as I,* from @.tab
> set @.int=@.int+1
> END
>|||I think that "the output is better than what I would expect" is VERY
debatable. I would expect an error stating that @.tab and @.i already
exist, just as if I had
declare @.i int
declare @.i int
in code somewhere. But because there is no block level scope the @.tab
and @.i are not freed and redeclared, so by putting the declares inside
the loop should result in an error.|||On 26 Apr 2005 11:35:06 -0700, Otter wrote:
>I think that "the output is better than what I would expect" is VERY
>debatable. I would expect an error stating that @.tab and @.i already
>exist, just as if I had
>declare @.i int
>declare @.i int
>in code somewhere. But because there is no block level scope the @.tab
>and @.i are not freed and redeclared, so by putting the declares inside
>the loop should result in an error.
Hi Otter,
The reason for this is that the declare statements are checked at parse
time, not at execution time. Parsing is just one scan over the code, from
top to bottom, disregarding any flow-of-control statements.
That's why this will work:
IF 1 = 2
BEGIN
DECLARE @.i int
END
SET @.i = 1
SELECT @.i
And this won't
IF 1 = 2
BEGIN
SET @.i = 1
END
DECLARE @.i int
SELECT @.i
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||They don't exactly get moved outside, but the declarations
are not executed statements. They are definitions that define
the context for the rest of the batch when executed. You can
see that they don't really "move" by looking at
while 1=0 begin
select @.i
declare @.i int
end
go
while 1=0 begin
declare @.i int
end
select @.i
The documentation is not very clear on this, but the scope of
variable declarations in SQL Server is from the point of declaration
in the text of the code downward to the end of the batch (first GO).
The execution order of statements in the code is irrelevant. It's the
code as text that matters, as seen here:
goto a
select @.x
a: declare @.x int
The environment (i.e., the variable declarations and values) are not
passed into subprograms like stored procedures or into queries
called with EXEC (<string> ), unless passed explicitly through an
available mechanism, like stored procedure parameters.
The behavior in T-SQL is not too far from the behavior in most
programming languages, except that scope does not end with
the end of a block like BEGIN .. END, only with the end of a
batch.
if 1=1 begin
declare @.i int
set @.i = 1
end else begin
declare @.j int
set @.j = 1
end
select @.i, @.j
If I remember my C++, for example, the variable i is not
redeclared with each loop iteration below, and that is the same
behavior we see in T-SQL. But i is not in scope below the
loop, and that is not the same behavior as in T-SQL. In
T-SQL there is no option to initialize a variable at declaration
like I'm doing here. All in all, there's no good reason I can think
of to declare a T-SQL variable inside a loop unless it puts
the variable closer to its use and readers will not think it
has C-like block scope.
[untested]
x = 10;
while --x {
int i = 0;
i += x;
cout << i
}
Steve Kass
Drew University
KH wrote:
>The declarations for the table and int variables get "moved" outside the lo
op
>by the compiler. I agree the output is not what I would expect either, but
I
>also think the output is better than what I would expect - i.e. SQL Server
is
>very "smart".
>
>"dott@.accessGeneral.com" wrote:
>
>sql
Interesting Query
I have four lookup tables that have identical structure. I have to
write a query to check if a particlaur string (code) exists in any of
the four lookups. What is the best way of dealing with this please?
1. Write one query with four corelated subqueries (one for each
lookup).
2. Write 4 separate queries and execute them one after the other.
If there is better way to store the lookups to make writing queries
like the one I have mentioned, easy, then I can change the lookup
tables.
ThanksIf all you have to test is existence, rather than return any value,
you could try something like:
SELECT CASE WHEN EXISTS(<query table 1> ) THEN 1
WHEN EXISTS(<query table 2> ) THEN 2
WHEN EXISTS(<query table 3> ) THEN 3
WHEN EXISTS(<query table 4> ) THEN 4
ELSE 0
END as Matched
Roy Harvey
Beacon Falls, CT
On 21 Apr 2006 03:43:14 -0700, "S Chapman" <s_chapman47@.hotmail.co.uk>
wrote:
>
>I have four lookup tables that have identical structure. I have to
>write a query to check if a particlaur string (code) exists in any of
>the four lookups. What is the best way of dealing with this please?
>1. Write one query with four corelated subqueries (one for each
>lookup).
>2. Write 4 separate queries and execute them one after the other.
>If there is better way to store the lookups to make writing queries
>like the one I have mentioned, easy, then I can change the lookup
>tables.
>Thanks|||Hi,
I'd rather create four left joins. Usually you need to return a value.
Tomasz B.
"Roy Harvey" wrote:
> If all you have to test is existence, rather than return any value,
> you could try something like:
> SELECT CASE WHEN EXISTS(<query table 1> ) THEN 1
> WHEN EXISTS(<query table 2> ) THEN 2
> WHEN EXISTS(<query table 3> ) THEN 3
> WHEN EXISTS(<query table 4> ) THEN 4
> ELSE 0
> END as Matched
> Roy Harvey
> Beacon Falls, CT
>
> On 21 Apr 2006 03:43:14 -0700, "S Chapman" <s_chapman47@.hotmail.co.uk>
> wrote:
>
>|||>> I have four lookup tables that have identical structure. I have to write
a query to check if a particular string (code) exists in any of the four loo
kups. What is the best way of dealing with this please? <<
The best way is not to split the encoding over four tables. Do you
also keep a personnel table for each employee's weight class?
This is a common design flaw called attribute splitting. You can build
a UNION-ed view and use it.
That view will also help show you that you have the same code with
different definitions in your data model (do you know the Patent Office
story?). Think you don't have this problem? Just wait. Or get the
extra overhead of prevetning it with triggers or other procedural,
proprietary code.
You will also have redundant data (look up a series of articles by Tom
Johnston on non-normal form redundancies).
Monday, March 19, 2012
intercept pipeline events programmatically
I'm wish to receive pipeline events fired by a SSIS package.
I execute the package successufully with the following code (c#):
MyEventListener eventListener = new XplorerEventListener();
DtsApplication app = new DtsApplication();
Package pkg = app.LoadPackage("c:\test.dstx", null);
pkg.Execute(null, null, eventListener, null, null);
MyEventListener is inherited from DefaultEvents, overriding all OnXXX methods.
It works perfectly, however I cannot intercept the following events:
- PipelineExecutionTrees
- PipelineExecutionPlan
- PipelineExecutionInitialization
- BufferSizeTuning
- PipelineInitialization
Anyone knows how to catch those pipeline events?
TIA,
Paolo.
I believe that at least some of those are log events and you need to intercept them by implementing an IDTSLogging interface.
Harry
Monday, March 12, 2012
Interactive Debugging of Stored Procedure
In VS 2005 with SQL Server Express: How do I debug a stored procedure that is called from a object datasource control while the asp.net code is running?
Thanks
You may need to PRINT debugging information in the stored procedure, or use SQL Profiler to capture a trace when executing the stored procedure.Friday, March 9, 2012
Intellisense for custom assemblies?
I am using a custom assembly with my reports.
When I reference my custom assembly using the =Code.<assembly reference>
syntax in the expressions in my report, Intellisense only shows the methods
(that I guess are) associated with the standard System.Object:
Equals
GetHashCode
GetType
ReferenceEquals
ToString
I do not see any of the Public methods defined in my custom assembly.
Is there a way to have that information provided within the Report Designer
when I use the "Edit Expression" dialog box, or is this a limitation in
Report Designer?
-- Chris
--
Chris, SSSIHello Chris,
This issue is a limitation in the Report Desinger. Since when you have put
some custom code in the report, the IDE could not compile and get the
method name immediately. You need to copy the method name to the expression
where you want to use the code.
If you have any concern about this issue, you could send your feedback
directly to the product team:
http://connect.microsoft.com/sqlserver/
Thank you for your understanding!
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================(This posting is provided "AS IS", with no warranties, and confers no
rights.)|||Hi ,
How is everything going? Please feel free to let me know if you need any
assistance.
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================This posting is provided "AS IS" with no warranties, and confers no rights.|||Wei Lu,
Thank you for your help. That is what I figured...that it is a limitation in
the product. Too bad, since having Intellisense support would be a nice
feature and would be consistent with the rest of the Visual Studio designers.
--
Chris, SSSI
"Wei Lu [MSFT]" wrote:
> Hello Chris,
> This issue is a limitation in the Report Desinger. Since when you have put
> some custom code in the report, the IDE could not compile and get the
> method name immediately. You need to copy the method name to the expression
> where you want to use the code.
> If you have any concern about this issue, you could send your feedback
> directly to the product team:
> http://connect.microsoft.com/sqlserver/
> Thank you for your understanding!
> Sincerely,
> Wei Lu
> Microsoft Online Community Support
> ==================================================> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications.
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/default.aspx.
> ==================================================> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>|||Hello Chris,
I would like to suggest you send your feedback to the product team.
http://connect.microsoft.com/sqlserver/
Thank you for your understanding!
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================This posting is provided "AS IS" with no warranties, and confers no rights.|||Hi Wei Lu,
>>I would like to suggest you send your feedback to the product team.
Is there a particular reason that you are not able to do this? I would think
Microsoft would want to proactively collect feedback on its products and not
have to rely on customers finding the time to report enhancement requests or
bugs on their own.
-- Chris
--
Chris, SSSI
"Wei Lu [MSFT]" wrote:
> Hello Chris,
> I would like to suggest you send your feedback to the product team.
> http://connect.microsoft.com/sqlserver/
> Thank you for your understanding!
> Sincerely,
> Wei Lu
> Microsoft Online Community Support
> ==================================================> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================> This posting is provided "AS IS" with no warranties, and confers no rights.
>|||Hi Chris,
When you send the feedback to the website, our product team will monitor
and consider the request.
I have send the feedback to the product team and they will consider this.
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================This posting is provided "AS IS" with no warranties, and confers no rights.
Integrity Violation when syncing with SQL CE, but not MSDE
Error Code: 80004005 Message : Run
NativeErr.: 28557
Source : Microsoft SQL Server 2000 Windows CE Edition
Err. Par. 0: data source=\Program Files\ThinkShare\Fdm\FdmJournal_eli.sdf; -
Error Code: 80040E2F
Message : The row update or insert cannot be reapplied due to an integrity
violation. [,,,,,]
NativeErr.: 28549 Source : Microsoft SQL Server 2000 Windows CE
We use merge replication and have several join and select filters. What's
interesting is that MSDE will sync just fine using the same publication.
Is it possible to find out what the offending table is and which row(s) is
causing the problem?
Thanks,
Eli
you might look for data types that were automatically converted during the
merge. For instance any nchar types longer tahtn 255 are converted to ntext
(which can't be a key). ther are a few others that I can't remember. they're
listed in online manuals.
Any of the offending columns can be remedied by modifying the article
properties in the publication setup. There you can turn of indexing
.....integrity settings...etc.. for articles in a publication.
i ran into these type of issues a few times and they were very frustrating
to figure out. If the database structure is intact after the faile merge
perhaps you can look at the local sqlce database with the ce query analyzer.
"Eli Tucker" <eli-msdn@.mailinator.com> wrote in message
news:Oz2$3I$gEHA.3320@.TK2MSFTNGP11.phx.gbl...
> I'm getting the following error when I do a sync with SQL CE:
> Error Code: 80004005 Message : Run
> NativeErr.: 28557
> Source : Microsoft SQL Server 2000 Windows CE Edition
> Err. Par. 0: data source=\Program
Files\ThinkShare\Fdm\FdmJournal_eli.sdf; -
> Error Code: 80040E2F
> Message : The row update or insert cannot be reapplied due to an integrity
> violation. [,,,,,]
> NativeErr.: 28549 Source : Microsoft SQL Server 2000 Windows CE
> We use merge replication and have several join and select filters. What's
> interesting is that MSDE will sync just fine using the same publication.
> Is it possible to find out what the offending table is and which row(s) is
> causing the problem?
> Thanks,
> Eli
>
|||I do have several columns that are varchar(256) that get converted to ntext
when syncing with SQL CE, but as far as I can tell none of these columns are
indexed or are keys. In fact, the sync works fine with some instances of
data, but not with others. When you were having the problem, did it happen
the first time you attempted to sync?
Also, could you explain how to turn off indexing/integrity settings of a
publication in more detail? I couldn't quote follow what you wrote below.
Thanks again for your help,
Eli
"mgarner1980" <mgarner@.kbsi.com> wrote in message
news:u34W8jKhEHA.3916@.TK2MSFTNGP11.phx.gbl...
> you might look for data types that were automatically converted during the
> merge. For instance any nchar types longer tahtn 255 are converted to
ntext
> (which can't be a key). ther are a few others that I can't remember.
they're
> listed in online manuals.
> Any of the offending columns can be remedied by modifying the article
> properties in the publication setup. There you can turn of indexing
> ....integrity settings...etc.. for articles in a publication.
> i ran into these type of issues a few times and they were very frustrating
> to figure out. If the database structure is intact after the faile merge
> perhaps you can look at the local sqlce database with the ce query
analyzer.[vbcol=seagreen]
> "Eli Tucker" <eli-msdn@.mailinator.com> wrote in message
> news:Oz2$3I$gEHA.3320@.TK2MSFTNGP11.phx.gbl...
> Files\ThinkShare\Fdm\FdmJournal_eli.sdf; -
integrity[vbcol=seagreen]
What's[vbcol=seagreen]
is
>