Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Friday, March 16, 2012

"System.InvalidOperationException: Invalid attempt to MetaData when reader is closed"

I am getting this exception sporadically in a certain block of code (it usually works):

"System.InvalidOperationException: Invalid attempt to MetaData when reader is closed"

The code is below: cmd.CommandText ="[dbo].CountryGetList"; cmd.CommandType = CommandType.StoredProcedure; IDataReader rdr = cmd.ExecuteReader(); List countries =new List(); ...while (rdr.Read()) {//do stuff                  

CountryID = (

Guid)rdr["CountryID"];

//blows up on the next line

cty.Name = (string)rdr["Name"];

}

} rdr.Close();

Any ideas!?

I guess thecmd.Connection has been closed before you finish reading data from IDataReader rdr

Tuesday, March 6, 2012

"ODBCcall failed" Error

Hi!
It has been weeks now that I have been trying to fix an error that I
encounter everytime it gets to the code at runtime:

SQLStmt = "SELECT Sum([Quantity in Stock]) As TotalInStock,
Sum([Cost] * [Quantity in Stock]) AS TotalCost " & _
"FROM [Inventory Products] WHERE [Item
Number] = '" & ItemNum & "'"

Set InvP = CurDB.OpenRecordset(SQLStmt, DB_OPEN_DYNASET) '
==>it gets the error here

I tried also to implement the same code using ADODB but still go the same
error: "ODBC--call failed".
There are other events where similar code is executed without any problems
at all.
Any guidance would be helpful.

ThanksBen,

The error is not very intuitive. You might want to try a SQL Trace and an
ODBC trace to get more information. Profiler has an Errors and Warnings /
Exception event you can trace, and the following link will show you how to
set up an ODBC trace:

http://support.microsoft.com/kb/274551
-- Bill

"Ben" <pillars4@.sbcglobal.netwrote in message
news:C7zxh.71351$qO4.32683@.newssvr13.news.prodigy. net...

Quote:

Originally Posted by

Hi!
It has been weeks now that I have been trying to fix an error that I
encounter everytime it gets to the code at runtime:
>
SQLStmt = "SELECT Sum([Quantity in Stock]) As TotalInStock,
Sum([Cost] * [Quantity in Stock]) AS TotalCost " & _
"FROM [Inventory Products] WHERE [Item
Number] = '" & ItemNum & "'"
>
Set InvP = CurDB.OpenRecordset(SQLStmt, DB_OPEN_DYNASET)
' ==>it gets the error here
>
I tried also to implement the same code using ADODB but still go the same
error: "ODBC--call failed".
There are other events where similar code is executed without any problems
at all.
Any guidance would be helpful.
>
>
Thanks
>

|||Ben (pillars4@.sbcglobal.net) writes:

Quote:

Originally Posted by

It has been weeks now that I have been trying to fix an error that I
encounter everytime it gets to the code at runtime:
>
SQLStmt = "SELECT Sum([Quantity in Stock]) As TotalInStock,
Sum([Cost] * [Quantity in Stock]) AS TotalCost " & _
"FROM [Inventory Products] WHERE [Item
Number] = '" & ItemNum & "'"
>
Set InvP = CurDB.OpenRecordset(SQLStmt, DB_OPEN_DYNASET) '
>==>it gets the error here
>
I tried also to implement the same code using ADODB but still go the same
error: "ODBC--call failed".
There are other events where similar code is executed without any problems
at all.


It would be interesting to see a little more of the code. How you set
up the command and so. Particularly when you do it with ADO. (Since I know
ADO better this other thing (DAO?)).

Have you extracted what is in SQLStmt an tried to run that in Query
Analyzer? Maybe there is some simple error?

Also, an advice on how you get ItemNum into the query string. Most APIs
support parameterised commands, for instance ADO does. Parameterised
is much simpler to use than interpolated strings, and it protexts you
against SQL Injection. It also uses the query cache in SQL Server better.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Saturday, February 25, 2012

"Name" Data Type?

Standard apologies for the extreme "newbie" nature of this question...
I've just seen a batch of sample code with the following declaration:
DECLARE @.MyVar Name
"@.MyVar", in this case, is there to receive some string data from an OPENXML
operation. The contents of the variable are subsequently used to feed other
functions that take varchar's as arguments. This works successfully.
The problem is, I've never heard of a "Name" before -- I can't CAST it to
anything useful, nor can I successfully concatenate it with a "+" operator t
o
other string data. I don't believe it is actually a data type, but I'm afrai
d
I don't know what it is, and because of what it's called it's darned hard to
look up in BOL!
Any insight would be greatly appreciated. Thanks!Dan,
A user-defined data type?
Try:
EXEC sp_datatype_info
GO
--and
EXEC sp_help
GO
HTH
Jerry
"Dan Costello" <DanCostello@.discussions.microsoft.com> wrote in message
news:C4A7366D-A8D9-4AE7-90D5-AF1E9CEDC847@.microsoft.com...
> Standard apologies for the extreme "newbie" nature of this question...
> I've just seen a batch of sample code with the following declaration:
> DECLARE @.MyVar Name
> "@.MyVar", in this case, is there to receive some string data from an
> OPENXML
> operation. The contents of the variable are subsequently used to feed
> other
> functions that take varchar's as arguments. This works successfully.
> The problem is, I've never heard of a "Name" before -- I can't CAST it to
> anything useful, nor can I successfully concatenate it with a "+" operator
> to
> other string data. I don't believe it is actually a data type, but I'm
> afraid
> I don't know what it is, and because of what it's called it's darned hard
> to
> look up in BOL!
> Any insight would be greatly appreciated. Thanks!|||Jerry,
Well, now I feel really stupid! Thanks, that's got it. For the sake of
completeness, the sample code uses the "AdventureWorks" sample database,
which does in fact have a user-defined type called "Name" already in it.
--Dan
"Jerry Spivey" wrote:

> Dan,
> A user-defined data type?
> Try:
> EXEC sp_datatype_info
> GO
> --and
> EXEC sp_help
> GO
> HTH
> Jerry
> "Dan Costello" <DanCostello@.discussions.microsoft.com> wrote in message
> news:C4A7366D-A8D9-4AE7-90D5-AF1E9CEDC847@.microsoft.com...
>
>

"Name" Data Type?

Standard apologies for the extreme "newbie" nature of this question...
I've just seen a batch of sample code with the following declaration:
DECLARE @.MyVar Name
"@.MyVar", in this case, is there to receive some string data from an OPENXML
operation. The contents of the variable are subsequently used to feed other
functions that take varchar's as arguments. This works successfully.
The problem is, I've never heard of a "Name" before -- I can't CAST it to
anything useful, nor can I successfully concatenate it with a "+" operator to
other string data. I don't believe it is actually a data type, but I'm afraid
I don't know what it is, and because of what it's called it's darned hard to
look up in BOL!
Any insight would be greatly appreciated. Thanks!Dan,
A user-defined data type?
Try:
EXEC sp_datatype_info
GO
--and
EXEC sp_help
GO
HTH
Jerry
"Dan Costello" <DanCostello@.discussions.microsoft.com> wrote in message
news:C4A7366D-A8D9-4AE7-90D5-AF1E9CEDC847@.microsoft.com...
> Standard apologies for the extreme "newbie" nature of this question...
> I've just seen a batch of sample code with the following declaration:
> DECLARE @.MyVar Name
> "@.MyVar", in this case, is there to receive some string data from an
> OPENXML
> operation. The contents of the variable are subsequently used to feed
> other
> functions that take varchar's as arguments. This works successfully.
> The problem is, I've never heard of a "Name" before -- I can't CAST it to
> anything useful, nor can I successfully concatenate it with a "+" operator
> to
> other string data. I don't believe it is actually a data type, but I'm
> afraid
> I don't know what it is, and because of what it's called it's darned hard
> to
> look up in BOL!
> Any insight would be greatly appreciated. Thanks!|||Jerry,
Well, now I feel really stupid! Thanks, that's got it. For the sake of
completeness, the sample code uses the "AdventureWorks" sample database,
which does in fact have a user-defined type called "Name" already in it.
--Dan
"Jerry Spivey" wrote:
> Dan,
> A user-defined data type?
> Try:
> EXEC sp_datatype_info
> GO
> --and
> EXEC sp_help
> GO
> HTH
> Jerry
> "Dan Costello" <DanCostello@.discussions.microsoft.com> wrote in message
> news:C4A7366D-A8D9-4AE7-90D5-AF1E9CEDC847@.microsoft.com...
> > Standard apologies for the extreme "newbie" nature of this question...
> >
> > I've just seen a batch of sample code with the following declaration:
> >
> > DECLARE @.MyVar Name
> >
> > "@.MyVar", in this case, is there to receive some string data from an
> > OPENXML
> > operation. The contents of the variable are subsequently used to feed
> > other
> > functions that take varchar's as arguments. This works successfully.
> >
> > The problem is, I've never heard of a "Name" before -- I can't CAST it to
> > anything useful, nor can I successfully concatenate it with a "+" operator
> > to
> > other string data. I don't believe it is actually a data type, but I'm
> > afraid
> > I don't know what it is, and because of what it's called it's darned hard
> > to
> > look up in BOL!
> >
> > Any insight would be greatly appreciated. Thanks!
>
>

"MOVE" and "FIND" in SQL Server 2000

Our code is made in asp
We are moving the data from access to sql-server.
The recordset methods "MOVE" and "FIND" is now returning errormessages.
Anyone who can tell us what to use instead - or where to look it up ?
Best Regards
Jens GjerloevWhat error message are you getting ?|||For instance:
RS.Find(name=) & name
RS.Move(25)
"Jensgjerloev" wrote:

> Our code is made in asp
> We are moving the data from access to sql-server.
> The recordset methods "MOVE" and "FIND" is now returning errormessages.
> Anyone who can tell us what to use instead - or where to look it up ?
> Best Regards
> Jens Gjerloev|||You need to post a piece of your code, as well as your error messages.
Jensgjerloev wrote:
> Our code is made in asp
> We are moving the data from access to sql-server.
> The recordset methods "MOVE" and "FIND" is now returning errormessages.
> Anyone who can tell us what to use instead - or where to look it up ?
> Best Regards
> Jens Gjerloev|||Hello to Dave & Jens
Thanks for your reply.
Let me be honest: we have been in a state of panic during migration
from access to sql - so the mentioned error had NO relation fo MOVE and/or
FIND.
Sorry -
best regards
Jens Gjerloev
"Dave Markle" <"dma[remove_ZZ]ZZrkle" wrote:

> You need to post a piece of your code, as well as your error messages.
> Jensgjerloev wrote:
>

"Login Failed" for SQL Express

Whenever I try to connect to my SQL Express Database with C# code,(.net 2.0), it says "Login failed for user 'TUser'. The user is not associated with a trusted SQL Server connection."

But I can connect with SQL Express Manager.

I've heard somewhere this is a SQL authentication problem, I didn't enable it during installation.

So is there a way to fix this? How can I enable SQL authentication without uninstalling it?

Is there anyone to help me?

Sunday, February 19, 2012

"Hidden" Replication Stored Procedures?

In looking thru the code for the replication related stored procedure
sp_addarticle, I noticed that there were 2 stored procedures called from
sp_addarticle that I can't seem to find anywhere on my SQL Server:
sys.sp_MSrepl_getpublisherinfo & sys.sp_MSrepl_addarticle. These are stored
procedures that appear to be undocumented and I have read comments on
Microsoft sites stating things like: “Undocumented stored procedures are only
for the use of internal replication components and should not be used to
administer replication.”. I just want to know more about these stored procs
(and others like these) as far as where they live, what they do, how
sp_addarticle calls these Stored Procs without the SP blowing up....as I
have tried to call sys.sp_MSrepl_getpublisherinfo in a 10 line script I made
and all I got back was that the sys.sp_MSrepl_getpublisherinfo couldn't be
found no matter what DB I tried to execute against. I don't see anything
helpful about these SPs when I Google (and I am a Google master) or anywhere
else. If anyone has anything relevant & helpful, I would appreciate the info.
Thanks.
Chris Jones
Application Development Consultant
they are in mssqlsystemresource.mdf. Open it in a text editor to read what
this proc does.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Chris" <Chris@.discussions.microsoft.com> wrote in message
news:420DD987-9E55-46AB-8445-82FD01740996@.microsoft.com...
> In looking thru the code for the replication related stored procedure
> sp_addarticle, I noticed that there were 2 stored procedures called from
> sp_addarticle that I can't seem to find anywhere on my SQL Server:
> sys.sp_MSrepl_getpublisherinfo & sys.sp_MSrepl_addarticle. These are
> stored
> procedures that appear to be undocumented and I have read comments on
> Microsoft sites stating things like: "Undocumented stored procedures are
> only
> for the use of internal replication components and should not be used to
> administer replication.". I just want to know more about these stored
> procs
> (and others like these) as far as where they live, what they do, how
> sp_addarticle calls these Stored Procs without the SP blowing up....as I
> have tried to call sys.sp_MSrepl_getpublisherinfo in a 10 line script I
> made
> and all I got back was that the sys.sp_MSrepl_getpublisherinfo couldn't be
> found no matter what DB I tried to execute against. I don't see anything
> helpful about these SPs when I Google (and I am a Google master) or
> anywhere
> else. If anyone has anything relevant & helpful, I would appreciate the
> info.
> --
> Thanks.
> Chris Jones
> Application Development Consultant
|||Great, that was exactly what I was looking for and when opening the
aforementioned MDF file in Notepad and doing a quick search, I found the SPs
I had mentioned in my earlier post. Thanks for the good info!
I have one follow up question based on your answer:
Is there a way to have the Resource Database appear in the list of Databases
in SQL Server Management Studio for easy browsing of it's contents, or is the
only browse option to open the MDF file with a text editor such as notepad?
Thanks.
Chris Jones
Application Development Consultant
"Hilary Cotter" wrote:

> they are in mssqlsystemresource.mdf. Open it in a text editor to read what
> this proc does.
> --
> Hilary Cotter
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
>
> "Chris" <Chris@.discussions.microsoft.com> wrote in message
> news:420DD987-9E55-46AB-8445-82FD01740996@.microsoft.com...
>
>
|||Just make a copy of the database files, attach it with a different name
and... 'voila'!
:-)
Chris wrote:[vbcol=seagreen]
> Great, that was exactly what I was looking for and when opening the
> aforementioned MDF file in Notepad and doing a quick search, I found the SPs
> I had mentioned in my earlier post. Thanks for the good info!
> I have one follow up question based on your answer:
> Is there a way to have the Resource Database appear in the list of Databases
> in SQL Server Management Studio for easy browsing of it's contents, or is the
> only browse option to open the MDF file with a text editor such as notepad?
> --
> Thanks.
> Chris Jones
> Application Development Consultant
>
> "Hilary Cotter" wrote:
|||Yip...you are absolutely correct...I just discovered that and was coming
back here to post the instructions. Here's what I found...
First, giving credit where credit is due, I found the following info I am
about to post at the following location:
http://www.aspfaq.com/sql2005/show.asp?id=28
Since the engine has hooks that hide the mssqlsystemresource database from
users, you don't have direct access to it through the GUI.
However, there is a way around this:
1.)Determine where the system databse files live, and keep this path handy:
USE master
GO
SELECT REPLACE(filename, 'master.mdf', '')
FROM sys.sysfiles
WHERE fileid = 1
2.)Stop the SQL Server service;
3.)Copy the files mssqlsystemresource.*df -> resource_copy.*df.
NOTE: do *not* rename or remove the mssqlsystemresource files!
4.)Start the SQL Server service;
5.)Run the following code in a new query window:
EXEC sp_attach_db
'Resource_Copy',
'<path from above>resource_copy.mdf',
'<path from above>resource_copy.ldf'
6.)Now, the system will no longer identify this database as a "special"
database
7.)You can easily scan through the names of system objects that might
otherwise be unknown (and browse any code associated with these objects) from
a GUI (like SQL Server Management Studio).
Thanks.
Chris Jones
Application Development Consultant
"zerg2k@.yahoo.com" wrote:

> Just make a copy of the database files, attach it with a different name
> and... 'voila'!
> :-)
> Chris wrote:
>
|||i just read ur post and i liked it beacuse i didn't even knew that SQL Server
uses such databases other then the known ones. but i didn't find the
mssqlsystemresource.mdf file can i please specify the address.
"Hilary Cotter" wrote:

> they are in mssqlsystemresource.mdf. Open it in a text editor to read what
> this proc does.
> --
> Hilary Cotter
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
>
> "Chris" <Chris@.discussions.microsoft.com> wrote in message
> news:420DD987-9E55-46AB-8445-82FD01740996@.microsoft.com...
>
>

Thursday, February 16, 2012

"For each row" Trigger

Hi All,
The trigger in the following code is working on only last record
(sorry row) inserted . I wish to process "EACH ROW" in trigger as in
Oracle but without using cursor.
create table employee
(
EmpId int,
FirstName varchar(20),
LastName varchar(20),
Supervisor int
)
create table TreeEmployee
(
EmpId int,
FirstName varchar(20),
LastName varchar(20),
Supervisor int
)
GO
create trigger trgInsertOn on TreeEmployee for insert
as
BEGIN
DECLARE @.EMPID INT
DECLARE @.SUPERVISOR INT
SELECT @.EMPID = EMPID ,@.SUPERVISOR = SUPERVISOR FROM INSERTED
SELECT 'INSERTINTG',@.EMPID , @.SUPERVISOR
IF @.EMPID IS NOT NULL
BEGIN
INSERT INTO TreeEmployee SELECT * FROM employee WHERE SUPERVISOR =
@.EMPID
END
END
GO
insert into employee
select 1,'Carl','Hogans',12
union
select 12,'Fred','Smith',NULL
union
select 16,'Sue','Bankers',1
union
select 26,'Frank','Green',12
union
select 55,'Karen','Feeders',NULL
union
select 56,'James','Black',12
union
select 57,'Kirk','Simmons',56
union
select 58,'Cliff','Page', 56
union
select 59,'Jimmy','Plant',56
union
select 60,'Jack','Cale', 59
union
select 61,'Robert','Santana',NULL
union
select 62,'Jack','Russell',1
INSERT INTO TreeEmployee SELECT * FROM employee WHERE EMPID = 12
SELECT * FROM TreeEmployee ORDER BY EMPID, Supervisor
DROP TRIGGER TRGINSERTON
Drop table TreeEmployee
DROP table employee
With warm regards
Jatinder SinghINSERT INTO TreeEmployee
SELECT * FROM employee WHERE SUPERVISOR IN
(
Select EmpId from Inserted where Empid IS NOT NULL
)
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"jsfromynr" wrote:

> Hi All,
> The trigger in the following code is working on only last record
> (sorry row) inserted . I wish to process "EACH ROW" in trigger as in
> Oracle but without using cursor.
> create table employee
> (
> EmpId int,
> FirstName varchar(20),
> LastName varchar(20),
> Supervisor int
> )
> create table TreeEmployee
> (
> EmpId int,
> FirstName varchar(20),
> LastName varchar(20),
> Supervisor int
> )
> GO
> create trigger trgInsertOn on TreeEmployee for insert
> as
> BEGIN
> DECLARE @.EMPID INT
> DECLARE @.SUPERVISOR INT
> SELECT @.EMPID = EMPID ,@.SUPERVISOR = SUPERVISOR FROM INSERTED
> SELECT 'INSERTINTG',@.EMPID , @.SUPERVISOR
> IF @.EMPID IS NOT NULL
> BEGIN
> INSERT INTO TreeEmployee SELECT * FROM employee WHERE SUPERVISOR =
> @.EMPID
> END
> END
> GO
> insert into employee
> select 1,'Carl','Hogans',12
> union
> select 12,'Fred','Smith',NULL
> union
> select 16,'Sue','Bankers',1
> union
> select 26,'Frank','Green',12
> union
> select 55,'Karen','Feeders',NULL
> union
> select 56,'James','Black',12
> union
> select 57,'Kirk','Simmons',56
> union
> select 58,'Cliff','Page', 56
> union
> select 59,'Jimmy','Plant',56
> union
> select 60,'Jack','Cale', 59
> union
> select 61,'Robert','Santana',NULL
> union
> select 62,'Jack','Russell',1
> INSERT INTO TreeEmployee SELECT * FROM employee WHERE EMPID = 12
> SELECT * FROM TreeEmployee ORDER BY EMPID, Supervisor
> DROP TRIGGER TRGINSERTON
> Drop table TreeEmployee
> DROP table employee
> With warm regards
> Jatinder Singh
>|||What exactly are you trying to do? Maybe you should first read a book or two
an trees and hierarchies.
When trying to build trees with triggers you need to consider the fact that
SQL prevents execution of recursive triggers, and consider the fact that the
maximum nesting level for recursions in SQL 2000 is 32.
ML|||Hi ML,
Yes , you are right .
I had read the articles written by Joe Celko .
'IF @.EMPID IS NOT NULL ' will stop the insert if empid is null
and we can add if TRIGGER_NESTLEVEL(object_id('YourTrigger
Name')) =32
return . Can't we? I am looking for an alternative approach .
With warm regards
Jatinder Singh|||First it's imperative that you design a good tree model on paper. One of the
most important requirements of any data tree IMHO is to prevent circular
referencing.
In your case there are two entities you need to design appropriate storage
for:
1) employees - selecting a unique key for each employee (a primary key,
maybe) is imperative; and
2) employee_hierarchy - not only do employees supervise other employees and
answer to another employee, relationships may also be contextual (roles,
projects) - in this case hierarchy instances must be supported.
Think about that. Entity #2 is a data tree which references employees, and
is also a self-referenced entity.
Look at this example in my blog:
http://milambda.blogspot.com/2005/0...or-monkeys.html
You can use the function to create your own 'hiearchy-discovery' methods.
ML|||Hi ML,
Really Your blog is excellent!! What I can say about that ? I
read it earlier .
With warm regards
Jatinder Singh|||:)
I'm glad you find it useful.
ML|||I might be missing something here, but here it goes...
When you write a trigger it only fires once per 'triggering' event. So in
your trigger below, only one insert is being run, only one trigger will run.
Where are all my records? Triggers expost an 'inserted' table. Inside this
special table are all of the records that were inserted. There is also a
table called deleted, but for updates triggers use deleted and inserted
(this is a seperate topic).
So onto the meat and potatos, you need to treat the inserted table as a
table! It has multiple rows. So you have two options as I see it.
1) Use a cursor to iterate over each row in the instered table (yuk)
2) Rewrite your trigger to support multiple records.
Here's my stab at #2:
CREATE TRIGGER trgInsertOn ON employee
FOR INSERT
AS
BEGIN
INSERT INTO TreeEmployee (EmpId, FirstName, LastName, Supervisor)
SELECT EmpId, FirstName, LastName, Supervisor
FROM inserted
WHERE supervisor IS NOT NULL
END
GO
Now, having done that I have a few remarks... You shouldn't be storing
anything in the supervisor table other than the EmpID and SupID. They
should both be FK's back to the employee table. Also, the trigger needs to
be on the insert of the employee table NOT the TreeEmployee table.
I'm really not sure what your ultimate goal is here. Your TreeEmployee
simply replicates your employee table except where EmpId is null...
That should get you going!
HTH,
Ben
"jsfromynr" <jatinder.singh@.clovertechnologies.com> wrote in message
news:1123066839.814729.101130@.g49g2000cwa.googlegroups.com...
> Hi All,
> The trigger in the following code is working on only last record
> (sorry row) inserted . I wish to process "EACH ROW" in trigger as in
> Oracle but without using cursor.
> create table employee
> (
> EmpId int,
> FirstName varchar(20),
> LastName varchar(20),
> Supervisor int
> )
> create table TreeEmployee
> (
> EmpId int,
> FirstName varchar(20),
> LastName varchar(20),
> Supervisor int
> )
> GO
> create trigger trgInsertOn on TreeEmployee for insert
> as
> BEGIN
> DECLARE @.EMPID INT
> DECLARE @.SUPERVISOR INT
> SELECT @.EMPID = EMPID ,@.SUPERVISOR = SUPERVISOR FROM INSERTED
> SELECT 'INSERTINTG',@.EMPID , @.SUPERVISOR
> IF @.EMPID IS NOT NULL
> BEGIN
> INSERT INTO TreeEmployee SELECT * FROM employee WHERE SUPERVISOR =
> @.EMPID
> END
> END
> GO
> insert into employee
> select 1,'Carl','Hogans',12
> union
> select 12,'Fred','Smith',NULL
> union
> select 16,'Sue','Bankers',1
> union
> select 26,'Frank','Green',12
> union
> select 55,'Karen','Feeders',NULL
> union
> select 56,'James','Black',12
> union
> select 57,'Kirk','Simmons',56
> union
> select 58,'Cliff','Page', 56
> union
> select 59,'Jimmy','Plant',56
> union
> select 60,'Jack','Cale', 59
> union
> select 61,'Robert','Santana',NULL
> union
> select 62,'Jack','Russell',1
> INSERT INTO TreeEmployee SELECT * FROM employee WHERE EMPID = 12
> SELECT * FROM TreeEmployee ORDER BY EMPID, Supervisor
> DROP TRIGGER TRGINSERTON
> Drop table TreeEmployee
> DROP table employee
> With warm regards
> Jatinder Singh
>|||Ok I think I see my mistake (maybe...) Here is my corrected trigger... If
this works, I'd tweak it to handle updates and deletes as well...
CREATE TRIGGER trgInsertOn ON employee
FOR INSERT
AS
BEGIN
-- start transaction to encapsulate the delete / insert
BEGIN TRANSACTION
-- to prevent dups, clear out inserted supervisors records from the tree
-- this also catches stale records
DELETE FROM TreeEmployee
WHERE Supervisor IN
(
SELECT Supervisor
FROM inserted
)
-- add anyone in the employee table to the TreeEmployee table
-- that has a newly inserted EmpId as a supervisor on them
INSERT INTO TreeEmployee (EmpId, FirstName, LastName, Supervisor)
SELECT EmpId, FirstName, LastName, Supervisor
FROM employee
WHERE supervisor IN
(
SELECT EmpId
FROM inserted
)
COMMIT TRANSACTION
END
GO
"Ben" <ben@.online.nospam> wrote in message
news:c060b$42f12956$d8445835$10658@.FUSE.NET...
>I might be missing something here, but here it goes...
> When you write a trigger it only fires once per 'triggering' event. So in
> your trigger below, only one insert is being run, only one trigger will
> run.
> Where are all my records? Triggers expost an 'inserted' table. Inside
> this special table are all of the records that were inserted. There is
> also a table called deleted, but for updates triggers use deleted and
> inserted (this is a seperate topic).
> So onto the meat and potatos, you need to treat the inserted table as a
> table! It has multiple rows. So you have two options as I see it.
> 1) Use a cursor to iterate over each row in the instered table (yuk)
> 2) Rewrite your trigger to support multiple records.
> Here's my stab at #2:
> CREATE TRIGGER trgInsertOn ON employee
> FOR INSERT
> AS
> BEGIN
> INSERT INTO TreeEmployee (EmpId, FirstName, LastName, Supervisor)
> SELECT EmpId, FirstName, LastName, Supervisor
> FROM inserted
> WHERE supervisor IS NOT NULL
> END
> GO
> Now, having done that I have a few remarks... You shouldn't be storing
> anything in the supervisor table other than the EmpID and SupID. They
> should both be FK's back to the employee table. Also, the trigger needs
> to be on the insert of the employee table NOT the TreeEmployee table.
> I'm really not sure what your ultimate goal is here. Your TreeEmployee
> simply replicates your employee table except where EmpId is null...
> That should get you going!
> HTH,
> Ben
>
>
> "jsfromynr" <jatinder.singh@.clovertechnologies.com> wrote in message
> news:1123066839.814729.101130@.g49g2000cwa.googlegroups.com...
>|||:( where does it end...
DELETE FROM TreeEmployee
WHERE Supervisor IN
(
SELECT Supervisor
FROM inserted
)
Needs to be
DELETE FROM TreeEmployee
WHERE Supervisor IN
(
SELECT EmpId
FROM inserted
)
"Ben" <ben@.online.nospam> wrote in message
news:9cfb4$42f12d04$d8445835$11906@.FUSE.NET...
> Ok I think I see my mistake (maybe...) Here is my corrected trigger...
> If this works, I'd tweak it to handle updates and deletes as well...
> CREATE TRIGGER trgInsertOn ON employee
> FOR INSERT
> AS
> BEGIN
> -- start transaction to encapsulate the delete / insert
> BEGIN TRANSACTION
> -- to prevent dups, clear out inserted supervisors records from the tree
> -- this also catches stale records
> DELETE FROM TreeEmployee
> WHERE Supervisor IN
> (
> SELECT Supervisor
> FROM inserted
> )
> -- add anyone in the employee table to the TreeEmployee table
> -- that has a newly inserted EmpId as a supervisor on them
> INSERT INTO TreeEmployee (EmpId, FirstName, LastName, Supervisor)
> SELECT EmpId, FirstName, LastName, Supervisor
> FROM employee
> WHERE supervisor IN
> (
> SELECT EmpId
> FROM inserted
> )
> COMMIT TRANSACTION
> END
> GO
>
> "Ben" <ben@.online.nospam> wrote in message
> news:c060b$42f12956$d8445835$10658@.FUSE.NET...
>

"File not created" from sp_trace_create

I am getting a return code of 12 (""File not created"") from sp_trace_create.

I have checked to ensure that the path is valid. I am using Windows Authentication and the Windows Administrator account.

SQL Server is using the Windows userid that I created for SQL Server. I have verified (at least tried to verify) that the user for SQL Server has access to the folder containing it.

The following is the relevant portion of what I am attempting.

DECLARE @.RC int, @.TraceId int
Exec @.RC = sp_trace_create @.TraceId OUTPUT, 0, N'C:\TraceFile'


I have tried other paths that also do not work.I used a path that was for a FAT partition and that worked, so it was a file permission problem. I don't know how to determine what path would be valid for the SQL Server account but that is a different question.

"Failed to open a rowset"

Help me... I have a report which I'm loading using the following code. When I deploy the application in a client's pc "Failed to open a rowset" is the error that appears. I am using an ODBC connection. Any help would be appreciated...

Dim cr As New crptCMR

Dim crtableLogoninfos As New TableLogOnInfos
Dim crtableLogoninfo As New TableLogOnInfo
Dim crConnectionInfo As New ConnectionInfo
Dim CrTables As Tables
Dim CrTable As Table
Dim TableCounter

Dim crReportDocument As New crptCMR

With crConnectionInfo
.ServerName = sname
.DatabaseName = dbname
.UserID = uid
.Password = pwd
End With

CrTables = crReportDocument.Database.Tables

For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next

CRVcmr.ReportSource = crReportDocumentOpen the report and do verify database

"Failed to obtain TransactionDispenserInterface"

This msg. appears in the startup messages in Errorlog.
Failed to obtain TransactionDispenserInterface: Result
Code = 0x8004d01b
It occurs about the 10th line in Errorlog and follows:
Attempting to initialize Distributed Transaction
Coordinator.
What causes it? How to fix it?
(SQL Server 7.0, SP4, NT 4.0)
Thank you.This error is reported if the MSDTC service is not running
when SQL starts. Makse sure the MSDTC service is
running and set it's startup to automatic if it's required
--
HTH
Jasper Smith (SQL Server MVP)
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"bd" <bruce.drechsler@.delphi.com> wrote in message
news:098c01c34bd0$69906f60$a001280a@.phx.gbl...
This msg. appears in the startup messages in Errorlog.
Failed to obtain TransactionDispenserInterface: Result
Code = 0x8004d01b
It occurs about the 10th line in Errorlog and follows:
Attempting to initialize Distributed Transaction
Coordinator.
What causes it? How to fix it?
(SQL Server 7.0, SP4, NT 4.0)
Thank you.|||MSDTC is not required at this time. I'll consider the
messages informational, only.
Thank you.
Bruce
>--Original Message--
>This error is reported if the MSDTC service is not running
>when SQL starts. Makse sure the MSDTC service is
>running and set it's startup to automatic if it's required
>--
>HTH
>Jasper Smith (SQL Server MVP)
>I support PASS - the definitive, global
>community for SQL Server professionals -
>http://www.sqlpass.org
>"bd" <bruce.drechsler@.delphi.com> wrote in message
>news:098c01c34bd0$69906f60$a001280a@.phx.gbl...
>This msg. appears in the startup messages in Errorlog.
>Failed to obtain TransactionDispenserInterface: Result
>Code = 0x8004d01b
>It occurs about the 10th line in Errorlog and follows:
>Attempting to initialize Distributed Transaction
>Coordinator.
>What causes it? How to fix it?
>(SQL Server 7.0, SP4, NT 4.0)
>Thank you.
>
>.
>

Saturday, February 11, 2012

"dbo" schema permissions question.

I have some ASP.NET 2.0 code executing
SqlDependency.Start(connectionstring) to a SQL 2005 database and I am
getting the following error:
System.Data.SqlClient.SqlException: The specified schema name "dbo"
either does not exist or you do not have permission to use it.
As a general SQL 2005 Security question, can anyone please inform me as
to how i would give my user permissions to the "dbo" schema? At
present the user has "dbo" set up as it's default schema, but I am not
sure what that does. Also my code works fine if I give the user
sysadmin rights.
Thanks,
ChrisHi Chris,
It would probably be better to have the user in SqlDependency.Start have a
different default schema than dbo (create schema foo authorization youruser;
alter user youruser with default_schema = foo). Otherwise you would have to
give it access to create objects in the dbo schema. Once you've done that,
the user needs CREATE SERVICE, CREATE QUEUE, and CREATE PROCEDURE. Because
SERVICE is at database scope (not schema scope) its queue or procedure
that's causing the error. Don't forget GRANT SUBSCRIBE QUERY NOTIFICATION to
the user executing the commands too.
Cheers,
Bob Beauchemin
http://www.SQLskills.com/blogs/bobb
"chris" <chris@.cubed-c.com> wrote in message
news:1138483703.893069.160850@.g43g2000cwa.googlegroups.com...
>I have some ASP.NET 2.0 code executing
> SqlDependency.Start(connectionstring) to a SQL 2005 database and I am
> getting the following error:
> System.Data.SqlClient.SqlException: The specified schema name "dbo"
> either does not exist or you do not have permission to use it.
> As a general SQL 2005 Security question, can anyone please inform me as
> to how i would give my user permissions to the "dbo" schema? At
> present the user has "dbo" set up as it's default schema, but I am not
> sure what that does. Also my code works fine if I give the user
> sysadmin rights.
> Thanks,
> Chris
>|||Bob,
First of all thanks for your article about this subject back when it
was still in beta.
Your solution worked! Very easy, thanks for your help.
Chris

"cursor" what is that

Can anyone tell me where i could learn about "cursors", what they're for and how to code it ?
Thanks a lot !Cursor is basically a loop through the data. Where you can stop and process each row in this loop separately. They are too slow and not recommended to use often.

Read more on declaration and use here:

http://msdn2.microsoft.com/en-us/library/ms190028.aspx
http://www.databasejournal.com/features/mssql/article.php/1439731

Good Luck.|||Thanks a lot iburyak !

"Could not continue scan with NOLOCK due to data" error during Replication Synchronization

I am receiving this message "Agent message code 601. Could not
continue scan with NOLOCK due to data" during the initial
synchronization after setting up a subscriber.
It is a straightforward tarnsactional replication of maybe twenty
tables.
I have never seen that message in the context of replication.
I think the 601 error looks to be slightly misleading as the previous
message in the log looks to indcate a successful snapshot
synchronization:
Delivered snapshot from the 'unc\serverX\20070208105750\' sub-folder
in 519625 milliseconds.
I have tried setting up the subscriber and publisher mulitple times
from scratch but always run into this message. Also all of the
replicated tables have multiple indexes and this is on a SQL Server
2005 SP1 Mirrored database. I have ran checkdb on database with no
problems found. This is halting the database from being replicated -
anyone know a way around this?
On Feb 8, 5:14 pm, "Calculated" <sarahjco...@.gmail.com> wrote:
> I am receiving this message "Agent message code 601.Could notcontinue scan with NOLOCK due to data"duringthe initialsynchronizationafter setting up a subscriber.
> It is a straightforward tarnsactionalreplicationof maybe twenty
> tables.
> I have never seen that message in the context ofreplication.
> I think the 601errorlooks to be slightly misleading as the previous
> message in the log looks to indcate a successful snapshotsynchronization:
> Delivered snapshot from the 'unc\serverX\20070208105750\' sub-folder
> in 519625 milliseconds.
> I have tried setting up the subscriber and publisher mulitple times
> from scratch but always run into this message. Also all of the
> replicated tables have multiple indexes and this is on a SQL Server
> 2005 SP1 Mirrored database. I have ran checkdb on database with no
> problems found. This is halting the database from being replicated -
> anyone know a way around this?
Resolved this - if anyone else has this problem try adding a table at
a time to the publication article and re-initializing the subscription
until you identify the problem table. Then either filter the table to
break down into smaller chunks (largest table in db was causing
problem in my case) or play around with the copy clustered index and
copy nonclustered index options on the specific table.

"Could not continue scan with NOLOCK due to data" error during Replication Synchronization

I am receiving this message "Agent message code 601. Could not
continue scan with NOLOCK due to data" during the initial
synchronization after setting up a subscriber.
It is a straightforward tarnsactional replication of maybe twenty
tables.
I have never seen that message in the context of replication.
I think the 601 error looks to be slightly misleading as the previous
message in the log looks to indcate a successful snapshot
synchronization:
Delivered snapshot from the 'unc\serverX\20070208105750\' sub-folder
in 519625 milliseconds.
I have tried setting up the subscriber and publisher mulitple times
from scratch but always run into this message. Also all of the
replicated tables have multiple indexes and this is on a SQL Server
2005 SP1 Mirrored database. I have ran checkdb on database with no
problems found. This is halting the database from being replicated -
anyone know a way around this?On Feb 8, 5:14 pm, "Calculated" <sarahjco...@.gmail.com> wrote:
> I am receiving this message "Agent message code 601.Could notcontinue scan with NOLOCK due to data"duringthe initialsynchronizationafter setting up a subscriber.
> It is a straightforward tarnsactionalreplicationof maybe twenty
> tables.
> I have never seen that message in the context ofreplication.
> I think the 601errorlooks to be slightly misleading as the previous
> message in the log looks to indcate a successful snapshotsynchronization:
> Delivered snapshot from the 'unc\serverX\20070208105750\' sub-folder
> in 519625 milliseconds.
> I have tried setting up the subscriber and publisher mulitple times
> from scratch but always run into this message. Also all of the
> replicated tables have multiple indexes and this is on a SQL Server
> 2005 SP1 Mirrored database. I have ran checkdb on database with no
> problems found. This is halting the database from being replicated -
> anyone know a way around this?
Resolved this - if anyone else has this problem try adding a table at
a time to the publication article and re-initializing the subscription
until you identify the problem table. Then either filter the table to
break down into smaller chunks (largest table in db was causing
problem in my case) or play around with the copy clustered index and
copy nonclustered index options on the specific table.

Friday, January 27, 2012

Hi. I have another problem. Anyone please?

Hi. I have another problem. Anyone please? Your answers will be greatly appreciated. My code is like this :
=iif(Fields!TDeact.Value = 0, "N/A",Fields!TDeactn.Value/Fields!TDeact.Value)
- this code is to eliminate the problem: "attempted to divide by zero";
however, it does not work. Still there's an error showing in my report, instead of the "N/A" i set in my code, it shows "#Error".
Anyone please, i have a deadline to meet for the realse of our project we're doing right now. Thanks.I think the issue is trying to have 2 separate data types to be displayed in
the same cell. You may try to change "N/A" to some other representation...
NaN etc...
"tulips_2812" wrote:
> Hi. I have another problem. Anyone please? Your answers will be greatly
> appreciated. My code is like this :
> =iif(Fields!TDeact.Value = 0, "N/A",
> Fields!TDeactn.Value/Fields!TDeact.Value)
> - this code is to eliminate the problem: "attempted to divide by zero";
> however, it does not work. Still there's an error showing in my report,
> instead of the "N/A" i set in my code, it shows "#Error".
> Anyone please, i have a deadline to meet for the realse of our project
> we're doing right now. Thanks.
>|||Try using 0, iif(deact.value = 0,0,deact.value)
--
U. Tokklas
"mike" wrote:
> I think the issue is trying to have 2 separate data types to be displayed in
> the same cell. You may try to change "N/A" to some other representation...
> NaN etc...
> "tulips_2812" wrote:
> > Hi. I have another problem. Anyone please? Your answers will be greatly
> > appreciated. My code is like this :
> >
> > =iif(Fields!TDeact.Value = 0, "N/A",
> > Fields!TDeactn.Value/Fields!TDeact.Value)
> >
> > - this code is to eliminate the problem: "attempted to divide by zero";
> > however, it does not work. Still there's an error showing in my report,
> > instead of the "N/A" i set in my code, it shows "#Error".
> >
> > Anyone please, i have a deadline to meet for the realse of our project
> > we're doing right now. Thanks.
> >
> >|||All parts of the IIF statement are evaluated, whether true or false.
You could write a code block to handle your problem. Search writing custom
code in the help.
=Code.Divide(Fields!TDeactn.Value,Fields!TDeact.Value)
Function Divide (byval nom as integer, byval denom as integer)
if denom > 0 then
return nom/denom
else
return "n/a"
end if
"Tokklas" wrote:
> Try using 0, iif(deact.value = 0,0,deact.value)
> --
> U. Tokklas
>
> "mike" wrote:
> > I think the issue is trying to have 2 separate data types to be displayed in
> > the same cell. You may try to change "N/A" to some other representation...
> > NaN etc...
> >
> > "tulips_2812" wrote:
> >
> > > Hi. I have another problem. Anyone please? Your answers will be greatly
> > > appreciated. My code is like this :
> > >
> > > =iif(Fields!TDeact.Value = 0, "N/A",
> > > Fields!TDeactn.Value/Fields!TDeact.Value)
> > >
> > > - this code is to eliminate the problem: "attempted to divide by zero";
> > > however, it does not work. Still there's an error showing in my report,
> > > instead of the "N/A" i set in my code, it shows "#Error".
> > >
> > > Anyone please, i have a deadline to meet for the realse of our project
> > > we're doing right now. Thanks.
> > >
> > >|||I wrote a piece of code:
Function DivideByZero(sValue1 as Object, sValue2 as Object)
If (sValue1 <> 0 AND sValue2 = 0) OR (sValue1 = 0 AND sValue2 <> 0) then
Return "NA"
Elseif sValue1 = 0 AND sValue2 = 0 then
Return "NA"
Else
Return (sValue1 - sValue2) / sValue2
End If
End Function
"tulips_2812" wrote:
> Hi. I have another problem. Anyone please? Your answers will be greatly
> appreciated. My code is like this :
> =iif(Fields!TDeact.Value = 0, "N/A",
> Fields!TDeactn.Value/Fields!TDeact.Value)
> - this code is to eliminate the problem: "attempted to divide by zero";
> however, it does not work. Still there's an error showing in my report,
> instead of the "N/A" i set in my code, it shows "#Error".
> Anyone please, i have a deadline to meet for the realse of our project
> we're doing right now. Thanks.
>|||Hello Harolds,
You're code was so fantastic. thank you very much. It really, really
helped..i just added End Function in the last part :)
Regards,
Althea|||I wrote a similar code. But I can not use this code in footer for calculate
sum.
Anyone solve this problem?
"Kerry" wrote:
> I wrote a piece of code:
> Function DivideByZero(sValue1 as Object, sValue2 as Object)
> If (sValue1 <> 0 AND sValue2 = 0) OR (sValue1 = 0 AND sValue2 <> 0) then
> Return "NA"
> Elseif sValue1 = 0 AND sValue2 = 0 then
> Return "NA"
> Else
> Return (sValue1 - sValue2) / sValue2
> End If
> End Function
> "tulips_2812" wrote:
> > Hi. I have another problem. Anyone please? Your answers will be greatly
> > appreciated. My code is like this :
> >
> > =iif(Fields!TDeact.Value = 0, "N/A",
> > Fields!TDeactn.Value/Fields!TDeact.Value)
> >
> > - this code is to eliminate the problem: "attempted to divide by zero";
> > however, it does not work. Still there's an error showing in my report,
> > instead of the "N/A" i set in my code, it shows "#Error".
> >
> > Anyone please, i have a deadline to meet for the realse of our project
> > we're doing right now. Thanks.
> >
> >|||Page header and footer have no access to the Fields collection.
You can have a textbox (may be hidden/invisible on a page and then aggregate
value of the textbox in page header/footer (Sum(ReportItems!mytextbox.Value)
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Savage" <Savage@.discussions.microsoft.com> wrote in message
news:14E1E6DB-7F3A-41A0-B415-4598BB8DF480@.microsoft.com...
>I wrote a similar code. But I can not use this code in footer for calculate
> sum.
> Anyone solve this problem?
> "Kerry" wrote:
>> I wrote a piece of code:
>> Function DivideByZero(sValue1 as Object, sValue2 as Object)
>> If (sValue1 <> 0 AND sValue2 = 0) OR (sValue1 = 0 AND sValue2 <> 0)
>> then
>> Return "NA"
>> Elseif sValue1 = 0 AND sValue2 = 0 then
>> Return "NA"
>> Else
>> Return (sValue1 - sValue2) / sValue2
>> End If
>> End Function
>> "tulips_2812" wrote:
>> > Hi. I have another problem. Anyone please? Your answers will be greatly
>> > appreciated. My code is like this :
>> >
>> > =iif(Fields!TDeact.Value = 0, "N/A",
>> > Fields!TDeactn.Value/Fields!TDeact.Value)
>> >
>> > - this code is to eliminate the problem: "attempted to divide by zero";
>> > however, it does not work. Still there's an error showing in my report,
>> > instead of the "N/A" i set in my code, it shows "#Error".
>> >
>> > Anyone please, i have a deadline to meet for the realse of our project
>> > we're doing right now. Thanks.
>> >
>> >|||I was talking about Table footer.
"Alexandre Mineev MSFT" wrote:
> Page header and footer have no access to the Fields collection.
> You can have a textbox (may be hidden/invisible on a page and then aggregate
> value of the textbox in page header/footer (Sum(ReportItems!mytextbox.Value)
> --
> This posting is provided "AS IS" with no warranties, and confers no rights.
> "Savage" <Savage@.discussions.microsoft.com> wrote in message
> news:14E1E6DB-7F3A-41A0-B415-4598BB8DF480@.microsoft.com...
> >I wrote a similar code. But I can not use this code in footer for calculate
> > sum.
> > Anyone solve this problem?
> >
> > "Kerry" wrote:
> >
> >> I wrote a piece of code:
> >>
> >> Function DivideByZero(sValue1 as Object, sValue2 as Object)
> >>
> >> If (sValue1 <> 0 AND sValue2 = 0) OR (sValue1 = 0 AND sValue2 <> 0)
> >> then
> >> Return "NA"
> >> Elseif sValue1 = 0 AND sValue2 = 0 then
> >> Return "NA"
> >> Else
> >> Return (sValue1 - sValue2) / sValue2
> >> End If
> >> End Function
> >>
> >> "tulips_2812" wrote:
> >>
> >> > Hi. I have another problem. Anyone please? Your answers will be greatly
> >> > appreciated. My code is like this :
> >> >
> >> > =iif(Fields!TDeact.Value = 0, "N/A",
> >> > Fields!TDeactn.Value/Fields!TDeact.Value)
> >> >
> >> > - this code is to eliminate the problem: "attempted to divide by zero";
> >> > however, it does not work. Still there's an error showing in my report,
> >> > instead of the "N/A" i set in my code, it shows "#Error".
> >> >
> >> > Anyone please, i have a deadline to meet for the realse of our project
> >> > we're doing right now. Thanks.
> >> >
> >> >
>
>|||The function DivideByZero should be Public and you should call it as
=Code.DivideByZero(x,y)
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Savage" <Savage@.discussions.microsoft.com> wrote in message
news:0EDDB40E-B10B-4D0C-9543-E4C07A984BB1@.microsoft.com...
>I was talking about Table footer.
>
> "Alexandre Mineev MSFT" wrote:
>> Page header and footer have no access to the Fields collection.
>> You can have a textbox (may be hidden/invisible on a page and then
>> aggregate
>> value of the textbox in page header/footer
>> (Sum(ReportItems!mytextbox.Value)
>> --
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>> "Savage" <Savage@.discussions.microsoft.com> wrote in message
>> news:14E1E6DB-7F3A-41A0-B415-4598BB8DF480@.microsoft.com...
>> >I wrote a similar code. But I can not use this code in footer for
>> >calculate
>> > sum.
>> > Anyone solve this problem?
>> >
>> > "Kerry" wrote:
>> >
>> >> I wrote a piece of code:
>> >>
>> >> Function DivideByZero(sValue1 as Object, sValue2 as Object)
>> >>
>> >> If (sValue1 <> 0 AND sValue2 = 0) OR (sValue1 = 0 AND sValue2 <> 0)
>> >> then
>> >> Return "NA"
>> >> Elseif sValue1 = 0 AND sValue2 = 0 then
>> >> Return "NA"
>> >> Else
>> >> Return (sValue1 - sValue2) / sValue2
>> >> End If
>> >> End Function
>> >>
>> >> "tulips_2812" wrote:
>> >>
>> >> > Hi. I have another problem. Anyone please? Your answers will be
>> >> > greatly
>> >> > appreciated. My code is like this :
>> >> >
>> >> > =iif(Fields!TDeact.Value = 0, "N/A",
>> >> > Fields!TDeactn.Value/Fields!TDeact.Value)
>> >> >
>> >> > - this code is to eliminate the problem: "attempted to divide by
>> >> > zero";
>> >> > however, it does not work. Still there's an error showing in my
>> >> > report,
>> >> > instead of the "N/A" i set in my code, it shows "#Error".
>> >> >
>> >> > Anyone please, i have a deadline to meet for the realse of our
>> >> > project
>> >> > we're doing right now. Thanks.
>> >> >
>> >> >
>>

Comment disables code?

It seem that using the old "--" style comment screws code in the IDE under
certain circumstances. As an example, the code in example one should create
a temporary table, populate it with the current int ID from a table along
with an int IDENTITY column that will serve as the new ID, then display the
resulting temporary table. The code runs as expected without the -- Comment
line, but as soon as it is added the table no longer populates, and the
temporary table is no longer displayed. The same type thing seems to happen
when you place -- comments following values in a SELECT statement as in
example two.
I ran the code under both Query Analyzer and the new 2005 IDE and they both
exhibited thje same behavior, but I've used the -- comment style for years
without an issue so I'm baffled. So far it only seems to affect SELECT
statements.
Has anyone else encountered this?
/* Example One */
PRINT '*** Create the LotEntity ID mapping table';
IF EXISTS(SELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME =
'LotEntity')
DROP TABLE tempdb.dbo.LotEntity
CREATE TABLE tempdb.dbo.LotEntity(OldID int, NewID int IDENTITY)
-- Comment
INSERT tempdb.dbo.LotEntity (OldID)
SELECT EntityID
FROM dbo.tbl_LotEntities
SELECT * FROM tempdb.dbo.LotEntity
/* Example 2 */
INSERT TableTwo (
ColumnOne,
ColumnTwo)
SELECT
ValueOne, -- ColumnOne
ValueTwo
FROM TableTwoI couldn't reproduce either of those problems.
For example 1, the problem probably not the comment, but that you do the SEL
ECT in the INSERT in the
same batch as the table is created. My guess is that the parses gets confuse
d when you in the same
batch creates a table, and then does a SELECT inside an INSERT where both of
these later statements
refer to the same table. That code generated an error (probably from the SEL
ECT inside the INSERT)
regardless of whether I have the comment or not.
For example 1, I got a proper error message stating that the table doesn't e
xist. And if I create
the table, no errors:
DROP TABLE TableTwo
GO
CREATE TABLE TableTwo (ColumnOne int, ColumnTwo int, ValueOne int, ValueTwo
int)
GO
INSERT TableTwo (
ColumnOne,
ColumnTwo)
SELECT
ValueOne, -- ColumnOne
ValueTwo
FROM TableTwo
If you can post a full repro, we can have a look at it. Oh, watch out for ed
itors etc that doesn't
produce a full CRLF. I've seen cases when you have only a CR (or was it LF)
which visually produces
a new line, but doesn't end the comment.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Byron" <Byron@.discussions.microsoft.com> wrote in message
news:BF89D3A3-A798-4220-852F-7A5FCC33971E@.microsoft.com...
> It seem that using the old "--" style comment screws code in the IDE under
> certain circumstances. As an example, the code in example one should crea
te
> a temporary table, populate it with the current int ID from a table along
> with an int IDENTITY column that will serve as the new ID, then display th
e
> resulting temporary table. The code runs as expected without the -- Comme
nt
> line, but as soon as it is added the table no longer populates, and the
> temporary table is no longer displayed. The same type thing seems to happ
en
> when you place -- comments following values in a SELECT statement as in
> example two.
> I ran the code under both Query Analyzer and the new 2005 IDE and they bot
h
> exhibited thje same behavior, but I've used the -- comment style for years
> without an issue so I'm baffled. So far it only seems to affect SELECT
> statements.
> Has anyone else encountered this?
>
> /* Example One */
> PRINT '*** Create the LotEntity ID mapping table';
> IF EXISTS(SELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
=
> 'LotEntity')
> DROP TABLE tempdb.dbo.LotEntity
> CREATE TABLE tempdb.dbo.LotEntity(OldID int, NewID int IDENTITY)
> -- Comment
> INSERT tempdb.dbo.LotEntity (OldID)
> SELECT EntityID
> FROM dbo.tbl_LotEntities
> SELECT * FROM tempdb.dbo.LotEntity
>
> /* Example 2 */
> INSERT TableTwo (
> ColumnOne,
> ColumnTwo)
> SELECT
> ValueOne, -- ColumnOne
> ValueTwo
> FROM TableTwo
>|||The example code stands alone, using a table valued variable in place of my
real table, but the results are the same. When Example 1 is run it correctl
y
retuns the two rows are expected. When Example 2 is run it does not.
Now the freaky part I just discovered while doing these examples. I can
copy the problematic code block including the comment out of my main query
into a new query window and it fails. I then copy it into Notepad then back
out into a query window and it works.
Some of the code was created using a stored procedure that built the SQL
strings based on table structures and I used CHAR(13) for CRLF to format the
code. Is it possible that the absence of CHAR(10) is confusing SQL? The
code was edited and saved in SQL Server Management Studio after it was
generated and the piece of code that is failing was not generated; it was
created by hand, but was added the file that had generated code in it .
/* Example 1 */
SET NOCOUNT ON
DECLARE @.t TABLE(ID int)
INSERT @.t VALUES(1)
INSERT @.t VALUES(2)
PRINT '*** Create the LotEntity ID mapping table';
IF EXISTS(SELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME =
'LotEntity')
DROP TABLE tempdb.dbo.LotEntity
CREATE TABLE tempdb.dbo.LotEntity(OldID int, NewID int IDENTITY)
INSERT tempdb.dbo.LotEntity (OldID)
SELECT ID
FROM @.t
SELECT * FROM tempdb.dbo.LotEntity
/* Example 1 results */
*** Create the LotEntity ID mapping table
OldID NewID
-- --
1 1
2 2
/* Example 2 */
SET NOCOUNT ON
DECLARE @.t TABLE(ID int)
INSERT @.t VALUES(1)
INSERT @.t VALUES(2)
PRINT '*** Create the LotEntity ID mapping table';
IF EXISTS(SELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME =
'LotEntity')
DROP TABLE tempdb.dbo.LotEntity
CREATE TABLE tempdb.dbo.LotEntity(OldID int, NewID int IDENTITY)
-- Comment
INSERT tempdb.dbo.LotEntity (OldID)
SELECT ID
FROM @.t
SELECT * FROM tempdb.dbo.LotEntity
/* Example 2 results */
*** Create the LotEntity ID mapping table
"Byron" wrote:

> It seem that using the old "--" style comment screws code in the IDE under
> certain circumstances. As an example, the code in example one should crea
te
> a temporary table, populate it with the current int ID from a table along
> with an int IDENTITY column that will serve as the new ID, then display th
e
> resulting temporary table. The code runs as expected without the -- Comme
nt
> line, but as soon as it is added the table no longer populates, and the
> temporary table is no longer displayed. The same type thing seems to happ
en
> when you place -- comments following values in a SELECT statement as in
> example two.
> I ran the code under both Query Analyzer and the new 2005 IDE and they bot
h
> exhibited thje same behavior, but I've used the -- comment style for years
> without an issue so I'm baffled. So far it only seems to affect SELECT
> statements.
> Has anyone else encountered this?
>
> /* Example One */
> PRINT '*** Create the LotEntity ID mapping table';
> IF EXISTS(SELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
=
> 'LotEntity')
> DROP TABLE tempdb.dbo.LotEntity
> CREATE TABLE tempdb.dbo.LotEntity(OldID int, NewID int IDENTITY)
> -- Comment
> INSERT tempdb.dbo.LotEntity (OldID)
> SELECT EntityID
> FROM dbo.tbl_LotEntities
> SELECT * FROM tempdb.dbo.LotEntity
>
> /* Example 2 */
> INSERT TableTwo (
> ColumnOne,
> ColumnTwo)
> SELECT
> ValueOne, -- ColumnOne
> ValueTwo
> FROM TableTwo
>|||> Some of the code was created using a stored procedure that built the SQL
> strings based on table structures and I used CHAR(13) for CRLF to format t
he
> code. Is it possible that the absence of CHAR(10) is confusing SQL?
Most probably. Make sure you have CHAR(13) + CHAR(10) and you should be fine
.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Byron" <Byron@.discussions.microsoft.com> wrote in message
news:ACFB2214-1B3A-4794-B5ED-FE8B55D0B167@.microsoft.com...
> The example code stands alone, using a table valued variable in place of m
y
> real table, but the results are the same. When Example 1 is run it correc
tly
> retuns the two rows are expected. When Example 2 is run it does not.
> Now the freaky part I just discovered while doing these examples. I can
> copy the problematic code block including the comment out of my main query
> into a new query window and it fails. I then copy it into Notepad then ba
ck
> out into a query window and it works.
> Some of the code was created using a stored procedure that built the SQL
> strings based on table structures and I used CHAR(13) for CRLF to format t
he
> code. Is it possible that the absence of CHAR(10) is confusing SQL? The
> code was edited and saved in SQL Server Management Studio after it was
> generated and the piece of code that is failing was not generated; it was
> created by hand, but was added the file that had generated code in it .
> /* Example 1 */
> SET NOCOUNT ON
> DECLARE @.t TABLE(ID int)
> INSERT @.t VALUES(1)
> INSERT @.t VALUES(2)
> PRINT '*** Create the LotEntity ID mapping table';
> IF EXISTS(SELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
=
> 'LotEntity')
> DROP TABLE tempdb.dbo.LotEntity
> CREATE TABLE tempdb.dbo.LotEntity(OldID int, NewID int IDENTITY)
> INSERT tempdb.dbo.LotEntity (OldID)
> SELECT ID
> FROM @.t
> SELECT * FROM tempdb.dbo.LotEntity
> /* Example 1 results */
> *** Create the LotEntity ID mapping table
> OldID NewID
> -- --
> 1 1
> 2 2
>
> /* Example 2 */
> SET NOCOUNT ON
> DECLARE @.t TABLE(ID int)
> INSERT @.t VALUES(1)
> INSERT @.t VALUES(2)
> PRINT '*** Create the LotEntity ID mapping table';
> IF EXISTS(SELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
=
> 'LotEntity')
> DROP TABLE tempdb.dbo.LotEntity
> CREATE TABLE tempdb.dbo.LotEntity(OldID int, NewID int IDENTITY)
> -- Comment
> INSERT tempdb.dbo.LotEntity (OldID)
> SELECT ID
> FROM @.t
> SELECT * FROM tempdb.dbo.LotEntity
> /* Example 2 results */
> *** Create the LotEntity ID mapping table
>
> "Byron" wrote:
>|||Probably a good Idea to store the CHAR(13) + CHAR(10) in a variable so you
are not having to do that many calls for every line of dynamic code you
create. Yes its subsecond but if you throw millions of records at the
procedure you could run into some issues with performance.
"Tibor Karaszi" wrote:

> Most probably. Make sure you have CHAR(13) + CHAR(10) and you should be fi
ne.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Byron" <Byron@.discussions.microsoft.com> wrote in message
> news:ACFB2214-1B3A-4794-B5ED-FE8B55D0B167@.microsoft.com...
>