Showing posts with label statement. Show all posts
Showing posts with label statement. Show all posts

Thursday, March 22, 2012

Data Driven Subscription with no results

I have a data driven subscription that is set up to email and uses a sql statement to generate the delivery settings and report parameters for each recipient (so each person does not reveive one email per row in the result set). I'm told that this is a good work around for creating a subscription that will NOT fire an email if there are no results returned, however I can not seem to get this to work...it continues to send an email with a blank report if there is no data for the previous day. Any thoughts/help would be appreciated. thanks in advance!If you want a fixed recipient list you want to do the following:

Assume your report data set comes from table DataTable, Pseudo SQL:
select Top 1 * from DataTable where InsertDate >= DATEADD(day, -1, GETDATE())

This will return exactly one row (one delivery) anytime there is data to report on. If you have multiple data sets in your report you'll need to play with the Joins to get this behavior.

Then in your subscription in the TO field, provide the static list of recipients.

If you need a dynamic list of recipients, I'd write a stored proc that returns the list of recipients if there was data in the DataTable, or no rows if there wasn't.

-Lukaszsql

Data Driven Query Task - question about Insert and Update queries

Does anyone know if, in an Update or Insert query within a DDQ task,
it's possible to have more than one statement in the query, and have
them both execute?
e.g.
update tablex set field1=1 where id=?
update tablex set field2 = 2 where id=?
Also, on the same subject, can you mix the statements, so let's say
even if it's the Update query you're using, could you tag an insert on
the end as well?
e.g.
update tablex set field1=1 where id=?
insert into tablex (field2) values (?)
Thanks for any help.Just as an afterthought, I suppose if this isn't possible it may be
useful to know whether the other two types of query can be 'hijacked'
and used as an extra insert (or whatever), but I'll wait to see if
anyone can confirm the original question or not before investigating
this one!!|||Quick note for anyone else looking at the same question: I've just
found the following on msdn:
"In order to refer to your SQL statements, you assign each statement a
name, called a query type. A query type, returned by your Microsoft=AE
ActiveX=AE script code, is used to select one of your SQL statements to
execute. Data Transformation Services (DTS) provides the following four
names:
Insert
Update
Delete
User
These query types should be viewed only as unique identifiers assigned
to each statement. It is in fact possible to perform any SQL operation
supported by the connection. It would be possible for example, to
perform four different updates, four different inserts or any mix of
these or stored procedures."
So I could do it by having an extra update as the 'delete' query, for
example. However, I'd feel it would be better 'practice' to have two
update statements within the Update query...I'd be grateful if anyone
with more experience than me could possibly provide an answer as to
whether you can have the two statements in the one query, as I've
trawled the internet and come up with nothing?
Many, many thanks indeed.,sql

Data Driven

Hi,
I understand I can create a stored procedure to return all the parameters
for data driven. The last statement of the Stored Procedure is Select *
DataDrivenTableName. However, after I put Exec SPName, and when i go to the
next page to fill out all the To, CC, Subject..., there is nothing in each
drop down box and I can't continue to finish the setup.
Any idea what's wrong.
Thanks
EdAFAIK RS uses first resultset to get data, not last.
Stjepan
"Ed" <Ed@.discussions.microsoft.com> wrote in message
news:2F0D6FE4-2FA8-49EB-A965-D0A57D47362C@.microsoft.com...
> Hi,
> I understand I can create a stored procedure to return all the parameters
> for data driven. The last statement of the Stored Procedure is Select *
> DataDrivenTableName. However, after I put Exec SPName, and when i go to
> the
> next page to fill out all the To, CC, Subject..., there is nothing in each
> drop down box and I can't continue to finish the setup.
> Any idea what's wrong.
> Thanks
> Ed

Wednesday, March 21, 2012

Data does not fit....

I am doing a simple update statement but am getting an error.

Cannot create a row of size 10675 which is greater than the allowable maximum of 8060.
The statement has been terminated.

I am inserting data that is as big as 7500 characters into a varchar(7500) field. I have made sure that my column is 7500 in length. the only way it fits is if I cut it down to 4950 characters...

Any Ideas?

William,

Total columns length for row is max 8060 (not a single column). Maybe you would want to put your new concatenated string into a text/ntext column.

PS: BTW check another thread about concatenating text fields earlier today.

|||

In SQL Server 2000, the entire row's data must be <= 8060 bytes, not just a single column. (this allows it to fit on a single page)

In SQL Server 2005, you can put > 8060 bytes on a row, but it is not advisable in most cases. Any rows that are larger than that spill over into a different page.

|||

I know about the limitation and I implement restrictors to make sure my working tables do not go over the given length that I need.

My data has a max length of 7500 because I populated the field with another process that only allows the data to be 7500 in length. So I do not know how the data is growing...

I am just setting a column (varchar 7500) to the value of another column (varchar 7500) and that is what does not make sense to me.

|||Can you post the script of the table?|||

this table gets populated by a process that limits the data to 7500

CREATE TABLE [dbo].[Search_hold] (
[id] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[credits] [varchar] (7500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]

When I do a simple update to this table is where I get the error... I am only posting the column that is effected because the table is 30+ columns wide....

CREATE TABLE [dbo].[Search] (
[prod_id] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[credits] [varchar] (7500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[srch_field] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[dateadded] [smalldatetime] NULL ,
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

The first table is to get the data and the main table is the second one. Alot more data is in the table that these columns...

Data Disappears on a RESTORE

I restore a database from one server to another using the RESTORE DATABASE statement followed by restoring all Tlogs.

The data in the retored database will be there for a few days but then
will just disapear!! I know it sounds crazy, but I have been retoring datbases from server to server and have not seen anything like this
before.

The database I am restoring is about 18 gb in size.

Any suggestions would be helpful as to why this might be happening.Originally posted by ToddBritt
I restore a database from one server to another using the RESTORE DATABASE statement followed by restoring all Tlogs. The data in the retored database will be there for a few days but then will just disapear!! I know it sounds crazy, but I have been retoring datbases from server to server and have not seen anything like this
before. The database I am restoring is about 18 gb in size.
Any suggestions would be helpful as to why this might be happening.

Q1 The data in the retored database will be there for a few days but then will just disapear!
A1 I doubt it somehow, when exactly, and how much, is it always the same tables? Collect more data on the issue to make some sense of what is happening.

Q2 Any suggestions would be helpful as to why this might be happening.
A2 There is insufficient information to suggest much of anything. However, you are / have checked integrity, etc., (say dbcc CheckDB) following your restores? How about running a row count proc following restore and running that daily, or add some delete triggers that record any login performing deletes.|||Thanks for your response.

Yes, after the restore I do a row count check followed by a DBCC CHECKDB -- could it have anything to do with the database being
in Bulk-Logged mode after the restore?

Should it be changed to Simple or Full Recovry mode?

I have not tried the DELETE Trigger idea.|||Originally posted by ToddBritt
Thanks for your response. Yes, after the restore I do a row count check followed by a DBCC CHECKDB -- could it have anything to do with the database being in Bulk-Logged mode after the restore?
Should it be changed to Simple or Full Recovry mode?
I have not tried the DELETE Trigger idea.

Q1 Could it have anything to do with the database being in Bulk-Logged mode after the restore?
A1 I can't intelligently assess the possibility (insufficient knowledge of the situation). {If you are bulk loading following a restore and that fails it might seem that data has disappeared relative to what you expect to be there I suppose.}

Q2 Should it be changed to Simple or Full Recovry mode?
A2 It depends on your situation.

S1 I suggest scheduling a periodic count (so you can identify when and how frequently data 'loss' occurs). Also implement delete triggers on tables that seem to have the 'unexplained loss' issue. You may also want to review which logins / accounts may run say drop table, truncate table, etc., and severely restict who has sufficient permissions to do such things.|||Originally posted by ToddBritt
could it have anything to do with the database being
in Bulk-Logged mode after the restore?

Should it be changed to Simple or Full Recovry mode?


Does it change to Bulk mode just by itself, after having restored??

The mode depends on your restore requirements. On "important" databases, I always have Full.

Friday, February 24, 2012

Dangers of update statement

I'm always a bit concerned when doing an update statement in case there is a
bug that causes it to update an entire table or too many records. Out of all
the programming techniques the update statement would have to have close to
the worst concequences for a bug, imo. I'm curious what techniques people
use to avoid this problem (besides the obvious such as testing).
The reason I'm asking is I found an update statement which should have had a
"where ID = @.ID" but I just plain forgot the where clause. This went out to
customers but through some miracle never got called. It was within a couple
of If statement and was only called in unusual circumstance which luckily
never happened.
Cheers,
MichaelMichael C wrote:
> I'm always a bit concerned when doing an update statement in case
> there is a bug that causes it to update an entire table or too many
> records. Out of all the programming techniques the update statement
> would have to have close to the worst concequences for a bug, imo.
> I'm curious what techniques people use to avoid this problem (besides
> the obvious such as testing).
> The reason I'm asking is I found an update statement which should
> have had a "where ID = @.ID" but I just plain forgot the where clause.
> This went out to customers but through some miracle never got called.
> It was within a couple of If statement and was only called in unusual
> circumstance which luckily never happened.
> Cheers,
> Michael
I'd say there was a serious gap in testing that particular procedure :-)
Every procedure should be tested with all possible inputs and the
outputs clearly examined and documented. As a part of the stored
procedure development process, the developer should clearly examine the
procedure code and design a set of test calls that will attack all of
the source. Also make sure that NULL parameter values are tested as well
where allowed. This could result in a lot of test calls, but it's much
easier to set this up during development than it is to design during an
application test. As you saw, it possible an application will never call
a stored procedure with the parameters that trigger a major problem. In
that case, the development side is the only way to catch these problems.
This could be an internal process where developers clearly document the
test cases (call text and expected outpu). You could document these
items along with the stored procedure text right in your version control
system. Another option is to use a framework for testing that can help
you automate unit testing. An open-source project call TSQL Unit is such
an option (although it hasn't been updated in a while):
http://msdn.microsoft.com/library/d...r />
p04i1.asp
http://sourceforge.net/projects/tsqlunit
That's not to say an error can't creep in occasionally, but a documented
development and testing process will go a long way in eliminating these
types of errors.
David Gugick
Quest Software
www.imceda.com
www.quest.com|||My post errored out so I am not sure if my earlies response got posted.
You could put begin tran before you update and commit or rollback tran after
checking your results.
Check out the following example:
set nocount on
go
create table test(
c1 int not null,
c2 int not null)
go
insert test values(1,1)
insert test values(2,1)
insert test values(3,1)
go
select * from test
go
begin tran
update test set c2= 333 where c1=2
select * from test
/*
c1 c2
-- --
1 1
2 333
3 1
*/
rollback tran
select * from test
/*
c1 c2
-- --
1 1
2 1
3 1
*/
go
drop table test
go
HTH...
http://zulfiqar.typepad.com
BSEE, MCP
"Michael C" wrote:

> I'm always a bit concerned when doing an update statement in case there is
a
> bug that causes it to update an entire table or too many records. Out of a
ll
> the programming techniques the update statement would have to have close t
o
> the worst concequences for a bug, imo. I'm curious what techniques people
> use to avoid this problem (besides the obvious such as testing).
> The reason I'm asking is I found an update statement which should have had
a
> "where ID = @.ID" but I just plain forgot the where clause. This went out t
o
> customers but through some miracle never got called. It was within a coupl
e
> of If statement and was only called in unusual circumstance which luckily
> never happened.
> Cheers,
> Michael
>
>|||On Fri, 19 Aug 2005 11:52:02 +1000, Michael C wrote:

>I'm always a bit concerned when doing an update statement in case there is
a
>bug that causes it to update an entire table or too many records. Out of al
l
>the programming techniques the update statement would have to have close to
>the worst concequences for a bug, imo.
Hi Michael,
I'd say that the DELETE is possibly even more dangerous.
In one of my first jobs in the SQL Server world, my boss was writing and
testing a query to remove erroneous rows from the production database
that were introduced by a bug. Here's how he tested his delete statement
to check that he got the where clause just right:
-- DELETE FROM TheTable
select * from TheTable
WHERE ....
AND ....
go
Once he had the wherer clause tweaked to return just the rows that had
to be deleted, he removed the comment in front of the first line and
clicked the execute button...
After a few minutes, he asked my more experienced coworker if he
understood why the query was taking so long. The coworker then
immediately rushed to his own PC and issued a kill command.
I think that this was the only time that we were actually glad that this
table was loaded with trigger code that was slower than a turtle with
two crippled legs.

> I'm curious what techniques people
>use to avoid this problem (besides the obvious such as testing).
For ad-hoc queries that I want to test first, I always START to type
this:
BEGIN TRAN
go
go
ROLLBACK TRAN
go
After that, I position the cursor between the two go's and start typing
the selects that show the result of my statement, and then the update,
insert, or delete statement itself.
(The extra go before the ROLLBACK ensures it gets executed even if the
query has an error that aborts the batch)
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:beecg1hp4o6lk251uv01l62c07trnm4rnb@.
4ax.com...
> I'd say that the DELETE is possibly even more dangerous.
> In one of my first jobs in the SQL Server world, my boss was writing and
> testing a query to remove erroneous rows from the production database
> that were introduced by a bug. Here's how he tested his delete statement
> to check that he got the where clause just right:
> -- DELETE FROM TheTable
> select * from TheTable
> WHERE ....
> AND ....
> go
We had a similar problem except much worse. One particular database had no
referential integrity and someone thought they'd issue a delete statement to
delete orphan records. Problem was they forgot the where clause altogether
and deleted all the records in the table. This went out to 300 customers but
I think the problem was found and fixed fairly quickly so not too many
people encountered the problem. I pointed out that the delete would have
failed if we had integrity but it seemed to fall on deaf ears.
Michael|||"David Gugick" <david.gugick-nospam@.quest.com> wrote in message
news:erhULaHpFHA.3376@.TK2MSFTNGP10.phx.gbl...
> I'd say there was a serious gap in testing that particular procedure :-)
> Every procedure should be tested with all possible inputs and the outputs
> clearly examined and documented. As a part of the stored procedure
> development process, the developer should clearly examine the procedure
> code and design a set of test calls that will attack all of the source.
> Also make sure that NULL parameter values are tested as well where
> allowed. This could result in a lot of test calls, but it's much easier to
> set this up during development than it is to design during an application
> test. As you saw, it possible an application will never call a stored
> procedure with the parameters that trigger a major problem. In that case,
> the development side is the only way to catch these problems.
You live in a very different world to me :-) Our projects are fairly rushed
and disorganised and testing is fairly poor. I'd love it to be different but
the only way to do that is change jobs.
Michael|||On Mon, 22 Aug 2005 13:57:38 +1000, Michael C wrote:
(snip)
>You live in a very different world to me :-) Our projects are fairly rushed
>and disorganised and testing is fairly poor. I'd love it to be different bu
t
>the only way to do that is change jobs.
Hi Michael,
But isn't the amount of time you have to spend repairing things even
more than the time you would have spent testing?
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:nulkg11ab4m7cr5dqiot9re6atbadmnkbo@.
4ax.com...
> But isn't the amount of time you have to spend repairing things even
> more than the time you would have spent testing?
That's a good question and I'd probably say yes but it's just the way it is
here. There's never any time to do it properly but there's always plenty of
time to fix it. There's always someone who needs something urgently and
always a reason to rush it. I keep explaining that if we'd been doing this
three years ago there'd be someone who needed it urgently then but no one
seems to listen to that. Nothing here get approved if it's over 2 months, it
doesn't matter if it runs over time as long the initial estimate is for 2
months or less. I could go on for hours about this. :-)
Michael|||Michael C wrote:
> We had a similar problem except much worse. One particular database had no
> referential integrity and someone thought they'd issue a delete statement
to
> delete orphan records. Problem was they forgot the where clause altogether
> and deleted all the records in the table. This went out to 300 customers b
ut
> I think the problem was found and fixed fairly quickly so not too many
> people encountered the problem. I pointed out that the delete would have
> failed if we had integrity but it seemed to fall on deaf ears.
> Michael
Hows the job hunting going? Seriously - do your customers know your
name, does your name get associated with these problems? Sooner or
later (probably), your customers are going to move to a supplier who
does care to get these things right in the first place. I'd get out
before the going gets bad (but make sure to include a good lengthy
sermon in your notice)
Just my two-penneth.
Damien|||Add this to your approval form:
1. This job must be done quick...
2. This job must be well done...
3. This job must be done cheap...
PICK 2 OUT OF 3.
Too bad a lot of bosses choose 1 and 3.
"Michael C" <mculley@.NOSPAMoptushome.com.au> wrote in message
news:%23DhnabQqFHA.1556@.TK2MSFTNGP12.phx.gbl...
> "Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
> news:nulkg11ab4m7cr5dqiot9re6atbadmnkbo@.
4ax.com...
> That's a good question and I'd probably say yes but it's just the way it
> is here. There's never any time to do it properly but there's always
> plenty of time to fix it. There's always someone who needs something
> urgently and always a reason to rush it. I keep explaining that if we'd
> been doing this three years ago there'd be someone who needed it urgently
> then but no one seems to listen to that. Nothing here get approved if it's
> over 2 months, it doesn't matter if it runs over time as long the initial
> estimate is for 2 months or less. I could go on for hours about this. :-)
> Michael
>

Friday, February 17, 2012

cycling through results of a select statement

I am new to stored procedures and T-SQL so please stick with me. I have a table that holds information about companies. I am trying to write a stored procedure that when run will query that table and find out if there are more than one entry of that company. All company names in that table must be unique (they can only occur once), if they occur more than once I need to flag it for reporting.

So what is the best way to go about this? Essentially what i was thinking was doing a select * on the table and then going from the first entry to the last and at each entry running a select * from table where companyname = @.nameofcompany. @.nameofcompany would be the name for that entry. If the select statement revealed more than one entry then i would know there was a problem.

Like I said I am new and this is probably very simple but i need a little help getting started

thanksSELECT *
FROM myTable99 o
WHERE EXISTS ( SELECT Company_Name
FROM myTable99 i
WHERE o.Company_Name = i.Company_Name
GROUP BY Company_Name
HAVING COUNT(*) > 1)

But you wouldn't have to do that if you defined the table like

CREATE TABLE myTable99(Company_Name varchar(50) UNIQUE)

EDIT: Where in Jersey? And what school?|||I am originally from Vernon (Mountain Creek). Went to school at Stevens Institute of Technology in Hoboken, NJ.

I didn't create the dll for this database so i just loaded the schema by the .sql file. Right now I am handed an excel template and i wrote somce vb code to go through that excel file pull out the information i want and then write it to a text file delimited with "#" and then i load it into SQL server with a bulk load command. If the user sends me a template that already has duplicate entries in it and i try to load the data into SQL column that has a unique indentifier what will happen? Will it throw an error? If this is the case then it would probably be better to get the data in the database and then decided whether or not it is a duplicate.
Redefining the table seems like the simplest way to go but i don't want to break functionality in the process

thanks|||I just tried to add this code and it is complaining on the second line in reference to the o. Here is the error: Error 170: Line 2: incorrect syntax near 'o'. As I said i am new to stored procedures and T-SQL do i need to declare the o and i as variables somewhere?|||I didn't test the code, so you gave me a start...but the code does work...

Where are you running this from? Do you have query analyzer and the other sql server client toools?

USE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myTable99 (Company_Name varchar(50))
GO

INSERT INTO myTable99(Company_Name)
SELECT 'Vernon Valley' UNION ALL
SELECT 'Mountain Creek' UNION ALL
SELECT 'Hidden Valley' UNION ALL
SELECT 'Campgaw' UNION ALL
SELECT 'Break Neck Road' UNION ALL
SELECT 'High Point' UNION ALL
SELECT 'Octogon Lounge' UNION ALL
SELECT 'Great Gorge' UNION ALL
SELECT 'Mountain Creek'
GO

SELECT *
FROM myTable99 o
WHERE EXISTS ( SELECT Company_Name
FROM myTable99 i
WHERE o.Company_Name = i.Company_Name
GROUP BY Company_Name
HAVING COUNT(*) > 1)
GO

SET NOCOUNT OFF
DROP TABLE myTable99
GO|||sorry i am an a** i have an extra space floating in there. Man i am an idiot|||Hey...you're from Jersey...never apologize