Showing posts with label stored. Show all posts
Showing posts with label stored. Show all posts

Friday, March 30, 2012

OUTPUT and Recordset

I have created a 'Intelligent' search stored procedure that accepts two input parameters SearchFor and SearchIn and an OUTPUT parameter @.Result

Basically it starts by looking for a match for the SearchFor and if more than 1 is found, returns a recordset of the matches so the user can select a SearchFor category, at the same time the OUTPUT parameter is set to let the calling script know what is being returned.

Likewise it does the same for the SearchIn until both are satisfied and the true search results recordset can be returned.

Is it possible to get an OUTPUT parameter AND a recordset at the same time?

Any help will be appreciated,

PhlazarI would put the results of the stored procedure into a global temp table....

Cheers
C

Wednesday, March 28, 2012

Outer table inside stored procedure

Hi,
I am using SQL Server 2000
I need to select values from a table that belongs to other database from
within a stored procedure. Hardcoding table's fully qualified name is not
appropriate. Table's database name must be a parameter of a stored procedure
.
Is there any better way than using dynamic query string generation? If query
is a dynamic string it is difficult to mainain it.
Thanks in advance.You could create a view which includes the database name - then when you nee
d
to change the database just change the view and all the SPs will reference
tthe correct database.
"Alexander Korol" wrote:

> Hi,
> I am using SQL Server 2000
> I need to select values from a table that belongs to other database from
> within a stored procedure. Hardcoding table's fully qualified name is not
> appropriate. Table's database name must be a parameter of a stored procedu
re.
> Is there any better way than using dynamic query string generation? If que
ry
> is a dynamic string it is difficult to mainain it.
> Thanks in advance.|||Sorry I made a multipost. Was not intended to just missed the window :) .
From now on let's refer to "How to refer from a stored procedure to a table
in another database"
Thanks for your reply.
I can not do anything beforehand. In the runtime my procedure can get only
the name of a database of guaranteed structure. Thereis a way to change the
database name in the view from stored procedure in the runtime ALTER VIEW. I
s
that what you ment? I will try.|||Dynamic altering the view helped. Thanks.
A very simple example:
exec('ALTER VIEW ViewName AS SELECT * FROM ' + @.NM_DATABASE +
'.dbo.TableName')

Monday, March 26, 2012

OUTER JOIN issue

The following stored procedure was created by one of our developers and it's
taking too long to be executed, we already identified that the section of th
e
coding with the problem are the outer joins, can anybody suggest something t
o
improve it?
Thanks
-- Populate performance layer table with latest data
----
SET @.StepCount = @.StepCount + 1
SET @.StepName = 'Populate performance layer table - part 1'
EXEC @.StepID = EDW_Manager.adm.get_Step_Reference @.ProcessID, @.StepName,
@.StepCount
SELECT isnull(isnull(isnull(poobd.clinical_hierarchy_ward_did,
unknown.clinical_hierarchy_ward_did), sday.clinical_hierarchy_ward_did),
sep.clinical_hierarchy_ward_did) as clinical_hierarchy_ward_did,
isnull(isnull(isnull(poobd.clinical_hierarchy_unit_did,
unknown.clinical_hierarchy_unit_did), sday.clinical_hierarchy_unit_did),
sep.clinical_hierarchy_unit_did) as clinical_hierarchy_unit_did,
isnull(isnull(isnull(poobd.cost_centre_did, unknown.cost_centre_did),
sday.cost_centre_did), sep.cost_centre_did) as cost_centre_did,
isnull(isnull(isnull(poobd.charge_cost_centre_did,
unknown.charge_cost_centre_did), sday.charge_cost_centre_did),
sep.charge_cost_centre_did) as charge_cost_centre_did,
isnull(isnull(isnull(poobd.budget_date_did, unknown.budget_date_did),
sday.budget_date_did), sep.budget_date_did) as budget_date_did,
isnull(isnull(isnull(poobd.initiative_did, unknown.initiative_did),
sday.initiative_did), sep.initiative_did) as initiative_did,
isnull(unknown.measure_UNKNOWN, 0) as measure_UNKNOWN,
isnull(poobd.measure_POOBD, 0) as measure_POOBD,
isnull(sday.measure_SDAY, 0) as measure_SDAY,
isnull(sep.measure_SEP, 0) as measure_SEP
INTO #activity_measures_part_1
FROM @.measure_UNKNOWN unknown
FULL OUTER JOIN @.measure_POOBD poobd
ON unknown.clinical_hierarchy_ward_did =
poobd.clinical_hierarchy_ward_did and
unknown.clinical_hierarchy_unit_did =
poobd.clinical_hierarchy_unit_did and
unknown.cost_centre_did = poobd.cost_centre_did and
unknown.charge_cost_centre_did = poobd.charge_cost_centre_did and
unknown.budget_date_did = poobd.budget_date_did and
unknown.initiative_did = poobd.initiative_did
FULL OUTER JOIN @.measure_SDAY sday
ON sday.clinical_hierarchy_ward_did =
poobd.clinical_hierarchy_ward_did and
sday.clinical_hierarchy_unit_did =
poobd.clinical_hierarchy_unit_did and
sday.cost_centre_did = poobd.cost_centre_did and
sday.charge_cost_centre_did = poobd.charge_cost_centre_did and
sday.budget_date_did = poobd.budget_date_did and
sday.initiative_did = poobd.initiative_did
FULL OUTER JOIN @.measure_SEP sep
ON sep.clinical_hierarchy_ward_did =
poobd.clinical_hierarchy_ward_did and
sep.clinical_hierarchy_unit_did =
poobd.clinical_hierarchy_unit_did and
sep.cost_centre_did = poobd.cost_centre_did and
sep.charge_cost_centre_did = poobd.charge_cost_centre_did and
sep.budget_date_did = poobd.budget_date_did and
sep.initiative_did = poobd.initiative_did
EXEC @.Error = EDW_Manager.adm.log_Step_Results @.StepID, @.@.ERROR, @.@.ROWCOUNT
IF (@.Error <> 0) GOTO LBL_Abort
SET @.StepCount = @.StepCount + 1
SET @.StepName = 'Populate performance layer table - part 2'
EXEC @.StepID = EDW_Manager.adm.get_Step_Reference @.ProcessID, @.StepName,
@.StepCount
SELECT isnull(isnull(isnull(soobd.clinical_hierarchy_ward_did,
wsep9.clinical_hierarchy_ward_did),ptobd.clinical_hierarchy_ward_did),stobd.
clinical_hierarchy_ward_did) as clinical_hierarchy_ward_did,
isnull(isnull(isnull(soobd.clinical_hierarchy_unit_did,
wsep9.clinical_hierarchy_unit_did),ptobd.clinical_hierarchy_unit_did),stobd.
clinical_hierarchy_unit_did) as clinical_hierarchy_unit_did,
isnull(isnull(isnull(soobd.cost_centre_did,
wsep9.cost_centre_did),ptobd.cost_centre_did),stobd.cost_centre_did) as
cost_centre_did,
isnull(isnull(isnull(soobd.charge_cost_centre_did,
wsep9.charge_cost_centre_did),ptobd.charge_cost_centre_did),stobd.charge_cos
t_centre_did) as charge_cost_centre_did,
isnull(isnull(isnull(soobd.budget_date_did,
wsep9.budget_date_did),ptobd.budget_date_did),stobd.budget_date_did) as
budget_date_did,
isnull(isnull(isnull(soobd.initiative_did,
wsep9.initiative_did),ptobd.initiative_did),stobd.initiative_did) as
initiative_did,
isnull(soobd.measure_SOOBD, 0) as measure_SOOBD,
isnull(wsep9.measure_WSEP9, 0) as measure_WSEP9,
isnull(ptobd.measure_PTOBD, 0) as measure_PTOBD,
isnull(stobd.measure_STOBD, 0) as measure_STOBD
INTO #activity_measures_part_2
FROM @.measure_SOOBD soobd
FULL OUTER JOIN @.measure_WSEP9 wsep9
ON wsep9.clinical_hierarchy_ward_did =
soobd.clinical_hierarchy_ward_did and
wsep9.clinical_hierarchy_unit_did =
soobd.clinical_hierarchy_unit_did and
wsep9.cost_centre_did = soobd.cost_centre_did and
wsep9.charge_cost_centre_did = soobd.charge_cost_centre_did and
wsep9.budget_date_did = soobd.budget_date_did and
wsep9.initiative_did = soobd.initiative_did
FULL OUTER JOIN @.measure_PTOBD ptobd
ON ptobd.clinical_hierarchy_ward_did =
soobd.clinical_hierarchy_ward_did and
ptobd.clinical_hierarchy_unit_did =
soobd.clinical_hierarchy_unit_did and
ptobd.cost_centre_did = soobd.cost_centre_did and
ptobd.charge_cost_centre_did = soobd.charge_cost_centre_did and
ptobd.budget_date_did = soobd.budget_date_did and
ptobd.initiative_did = soobd.initiative_did
FULL OUTER JOIN @.measure_STOBD stobd
ON stobd.clinical_hierarchy_ward_did =
soobd.clinical_hierarchy_ward_did and
stobd.clinical_hierarchy_unit_did =
soobd.clinical_hierarchy_unit_did and
stobd.cost_centre_did = soobd.cost_centre_did and
stobd.charge_cost_centre_did = soobd.charge_cost_centre_did and
stobd.budget_date_did = soobd.budget_date_did and
stobd.initiative_did = soobd.initiative_did
EXEC @.Error = EDW_Manager.adm.log_Step_Results @.StepID, @.@.ERROR, @.@.ROWCOUNT
IF (@.Error <> 0) GOTO LBL_Abort
select count(*) from #activity_measures_part_1
select count(*) from #activity_measures_part_2
SET @.StepCount = @.StepCount + 1
SET @.StepName = 'Populate performance layer table - final'
EXEC @.StepID = EDW_Manager.adm.get_Step_Reference @.ProcessID, @.StepName,
@.StepCount
INSERT INTO EDW_reporting.whs.PERF_activity_budget_ward_by_measure
(clinical_hierarchy_ward_did,
clinical_hierarchy_unit_did,
cost_centre_did,
charge_cost_centre_did,
budget_date_did,
initiative_did,
measure_UNKNOWN,
measure_POOBD,
measure_SDAY,
measure_SEP,
measure_SOOBD,
measure_WSEP9,
measure_PTOBDs,
measure_STOBDs,
batch_id)
SELECT isnull(part1.clinical_hierarchy_ward_did,
part2.clinical_hierarchy_ward_did) as clinical_hierarchy_ward_did,
isnull(part1.clinical_hierarchy_unit_did,
part2.clinical_hierarchy_unit_did) as clinical_hierarchy_unit_did,
isnull(part1.cost_centre_did, part2.cost_centre_did) as
cost_centre_did,
isnull(part1.charge_cost_centre_did, part2.charge_cost_centre_did) as
charge_cost_centre_did,
isnull(part1.budget_date_did, part2.budget_date_did) as
budget_date_did,
isnull(part1.initiative_did, part2.initiative_did) as initiative_did,
isnull(part1.measure_UNKNOWN, 0) as measure_UNKNOWN,
isnull(part1.measure_POOBD, 0) as measure_POOBD,
isnull(part1.measure_SDAY, 0) as measure_SDAY,
isnull(part1.measure_SEP, 0) as measure_SEP,
isnull(part2.measure_SOOBD, 0) as measure_SOOBD,
isnull(part2.measure_WSEP9, 0) as measure_WSEP9,
isnull(part2.measure_PTOBD, 0) as measure_PTOBD,
isnull(part2.measure_STOBD, 0) as measure_STOBD,
@.BatchID
FROM #activity_measures_part_1 part1
FULL OUTER JOIN #activity_measures_part_2 part2
ON part1.clinical_hierarchy_ward_did =
part2.clinical_hierarchy_ward_did and
part1.clinical_hierarchy_unit_did =
part2.clinical_hierarchy_unit_did and
part1.cost_centre_did = part2.cost_centre_did and
part1.charge_cost_centre_did = part2.charge_cost_centre_did and
part1.budget_date_did = part2.budget_date_did and
part1.initiative_did = part2.initiative_did
EXEC @.Error = EDW_Manager.adm.log_Step_Results @.StepID, @.@.ERROR, @.@.ROWCOUNT
IF (@.Error <> 0) GOTO LBL_AbortWithout the table structures, index details and size of tables it is very
difficult to say what is wrong - but it may be worth running it through the
index tuning wizard to see what it comes up with
Have a look at the execution plan and see where the expensive bits are.
May be worth replacing all those nested isnulls with a coalesce of all the
columns in the nested isnulls - makes it much easier to read, less code and
may help.
If no luck try posting the executiuon plan and ddl for all the tables -
someone may then have some more suggestions
Mike John
"FJC" <FJC@.discussions.microsoft.com> wrote in message
news:30F9AE02-48DD-4FC9-BD5C-636224FB5CE3@.microsoft.com...
> The following stored procedure was created by one of our developers and
> it's
> taking too long to be executed, we already identified that the section of
> the
> coding with the problem are the outer joins, can anybody suggest something
> to
> improve it?
> Thanks
> -- Populate performance layer table with latest data
> ----
> SET @.StepCount = @.StepCount + 1
> SET @.StepName = 'Populate performance layer table - part 1'
> EXEC @.StepID = EDW_Manager.adm.get_Step_Reference @.ProcessID, @.StepName,
> @.StepCount
> SELECT isnull(isnull(isnull(poobd.clinical_hierarchy_ward_did,
> unknown.clinical_hierarchy_ward_did), sday.clinical_hierarchy_ward_did),
> sep.clinical_hierarchy_ward_did) as clinical_hierarchy_ward_did,
> isnull(isnull(isnull(poobd.clinical_hierarchy_unit_did,
> unknown.clinical_hierarchy_unit_did), sday.clinical_hierarchy_unit_did),
> sep.clinical_hierarchy_unit_did) as clinical_hierarchy_unit_did,
> isnull(isnull(isnull(poobd.cost_centre_did, unknown.cost_centre_did),
> sday.cost_centre_did), sep.cost_centre_did) as cost_centre_did,
> isnull(isnull(isnull(poobd.charge_cost_centre_did,
> unknown.charge_cost_centre_did), sday.charge_cost_centre_did),
> sep.charge_cost_centre_did) as charge_cost_centre_did,
> isnull(isnull(isnull(poobd.budget_date_did, unknown.budget_date_did),
> sday.budget_date_did), sep.budget_date_did) as budget_date_did,
> isnull(isnull(isnull(poobd.initiative_did, unknown.initiative_did),
> sday.initiative_did), sep.initiative_did) as initiative_did,
> isnull(unknown.measure_UNKNOWN, 0) as measure_UNKNOWN,
> isnull(poobd.measure_POOBD, 0) as measure_POOBD,
> isnull(sday.measure_SDAY, 0) as measure_SDAY,
> isnull(sep.measure_SEP, 0) as measure_SEP
> INTO #activity_measures_part_1
> FROM @.measure_UNKNOWN unknown
> FULL OUTER JOIN @.measure_POOBD poobd
> ON unknown.clinical_hierarchy_ward_did =
> poobd.clinical_hierarchy_ward_did and
> unknown.clinical_hierarchy_unit_did =
> poobd.clinical_hierarchy_unit_did and
> unknown.cost_centre_did = poobd.cost_centre_did and
> unknown.charge_cost_centre_did = poobd.charge_cost_centre_did
> and
> unknown.budget_date_did = poobd.budget_date_did and
> unknown.initiative_did = poobd.initiative_did
> FULL OUTER JOIN @.measure_SDAY sday
> ON sday.clinical_hierarchy_ward_did =
> poobd.clinical_hierarchy_ward_did and
> sday.clinical_hierarchy_unit_did =
> poobd.clinical_hierarchy_unit_did and
> sday.cost_centre_did = poobd.cost_centre_did and
> sday.charge_cost_centre_did = poobd.charge_cost_centre_did and
> sday.budget_date_did = poobd.budget_date_did and
> sday.initiative_did = poobd.initiative_did
> FULL OUTER JOIN @.measure_SEP sep
> ON sep.clinical_hierarchy_ward_did =
> poobd.clinical_hierarchy_ward_did and
> sep.clinical_hierarchy_unit_did =
> poobd.clinical_hierarchy_unit_did and
> sep.cost_centre_did = poobd.cost_centre_did and
> sep.charge_cost_centre_did = poobd.charge_cost_centre_did and
> sep.budget_date_did = poobd.budget_date_did and
> sep.initiative_did = poobd.initiative_did
> EXEC @.Error = EDW_Manager.adm.log_Step_Results @.StepID, @.@.ERROR,
> @.@.ROWCOUNT
> IF (@.Error <> 0) GOTO LBL_Abort
> SET @.StepCount = @.StepCount + 1
> SET @.StepName = 'Populate performance layer table - part 2'
> EXEC @.StepID = EDW_Manager.adm.get_Step_Reference @.ProcessID, @.StepName,
> @.StepCount
> SELECT isnull(isnull(isnull(soobd.clinical_hierarchy_ward_did,
> wsep9.clinical_hierarchy_ward_did),ptobd.clinical_hierarchy_ward_did),stob
d.clinical_hierarchy_ward_did)
> as clinical_hierarchy_ward_did,
> isnull(isnull(isnull(soobd.clinical_hierarchy_unit_did,
> wsep9.clinical_hierarchy_unit_did),ptobd.clinical_hierarchy_unit_did),stob
d.clinical_hierarchy_unit_did)
> as clinical_hierarchy_unit_did,
> isnull(isnull(isnull(soobd.cost_centre_did,
> wsep9.cost_centre_did),ptobd.cost_centre_did),stobd.cost_centre_did) as
> cost_centre_did,
> isnull(isnull(isnull(soobd.charge_cost_centre_did,
> wsep9.charge_cost_centre_did),ptobd.charge_cost_centre_did),stobd.charge_c
ost_centre_did)
> as charge_cost_centre_did,
> isnull(isnull(isnull(soobd.budget_date_did,
> wsep9.budget_date_did),ptobd.budget_date_did),stobd.budget_date_did) as
> budget_date_did,
> isnull(isnull(isnull(soobd.initiative_did,
> wsep9.initiative_did),ptobd.initiative_did),stobd.initiative_did) as
> initiative_did,
> isnull(soobd.measure_SOOBD, 0) as measure_SOOBD,
> isnull(wsep9.measure_WSEP9, 0) as measure_WSEP9,
> isnull(ptobd.measure_PTOBD, 0) as measure_PTOBD,
> isnull(stobd.measure_STOBD, 0) as measure_STOBD
> INTO #activity_measures_part_2
> FROM @.measure_SOOBD soobd
> FULL OUTER JOIN @.measure_WSEP9 wsep9
> ON wsep9.clinical_hierarchy_ward_did =
> soobd.clinical_hierarchy_ward_did and
> wsep9.clinical_hierarchy_unit_did =
> soobd.clinical_hierarchy_unit_did and
> wsep9.cost_centre_did = soobd.cost_centre_did and
> wsep9.charge_cost_centre_did = soobd.charge_cost_centre_did and
> wsep9.budget_date_did = soobd.budget_date_did and
> wsep9.initiative_did = soobd.initiative_did
> FULL OUTER JOIN @.measure_PTOBD ptobd
> ON ptobd.clinical_hierarchy_ward_did =
> soobd.clinical_hierarchy_ward_did and
> ptobd.clinical_hierarchy_unit_did =
> soobd.clinical_hierarchy_unit_did and
> ptobd.cost_centre_did = soobd.cost_centre_did and
> ptobd.charge_cost_centre_did = soobd.charge_cost_centre_did and
> ptobd.budget_date_did = soobd.budget_date_did and
> ptobd.initiative_did = soobd.initiative_did
> FULL OUTER JOIN @.measure_STOBD stobd
> ON stobd.clinical_hierarchy_ward_did =
> soobd.clinical_hierarchy_ward_did and
> stobd.clinical_hierarchy_unit_did =
> soobd.clinical_hierarchy_unit_did and
> stobd.cost_centre_did = soobd.cost_centre_did and
> stobd.charge_cost_centre_did = soobd.charge_cost_centre_did and
> stobd.budget_date_did = soobd.budget_date_did and
> stobd.initiative_did = soobd.initiative_did
> EXEC @.Error = EDW_Manager.adm.log_Step_Results @.StepID, @.@.ERROR,
> @.@.ROWCOUNT
> IF (@.Error <> 0) GOTO LBL_Abort
>
> select count(*) from #activity_measures_part_1
> select count(*) from #activity_measures_part_2
> SET @.StepCount = @.StepCount + 1
> SET @.StepName = 'Populate performance layer table - final'
> EXEC @.StepID = EDW_Manager.adm.get_Step_Reference @.ProcessID, @.StepName,
> @.StepCount
> INSERT INTO EDW_reporting.whs.PERF_activity_budget_ward_by_measure
> (clinical_hierarchy_ward_did,
> clinical_hierarchy_unit_did,
> cost_centre_did,
> charge_cost_centre_did,
> budget_date_did,
> initiative_did,
> measure_UNKNOWN,
> measure_POOBD,
> measure_SDAY,
> measure_SEP,
> measure_SOOBD,
> measure_WSEP9,
> measure_PTOBDs,
> measure_STOBDs,
> batch_id)
> SELECT isnull(part1.clinical_hierarchy_ward_did,
> part2.clinical_hierarchy_ward_did) as clinical_hierarchy_ward_did,
> isnull(part1.clinical_hierarchy_unit_did,
> part2.clinical_hierarchy_unit_did) as clinical_hierarchy_unit_did,
> isnull(part1.cost_centre_did, part2.cost_centre_did) as
> cost_centre_did,
> isnull(part1.charge_cost_centre_did, part2.charge_cost_centre_did) as
> charge_cost_centre_did,
> isnull(part1.budget_date_did, part2.budget_date_did) as
> budget_date_did,
> isnull(part1.initiative_did, part2.initiative_did) as initiative_did,
> isnull(part1.measure_UNKNOWN, 0) as measure_UNKNOWN,
> isnull(part1.measure_POOBD, 0) as measure_POOBD,
> isnull(part1.measure_SDAY, 0) as measure_SDAY,
> isnull(part1.measure_SEP, 0) as measure_SEP,
> isnull(part2.measure_SOOBD, 0) as measure_SOOBD,
> isnull(part2.measure_WSEP9, 0) as measure_WSEP9,
> isnull(part2.measure_PTOBD, 0) as measure_PTOBD,
> isnull(part2.measure_STOBD, 0) as measure_STOBD,
> @.BatchID
> FROM #activity_measures_part_1 part1
> FULL OUTER JOIN #activity_measures_part_2 part2
> ON part1.clinical_hierarchy_ward_did =
> part2.clinical_hierarchy_ward_did and
> part1.clinical_hierarchy_unit_did =
> part2.clinical_hierarchy_unit_did and
> part1.cost_centre_did = part2.cost_centre_did and
> part1.charge_cost_centre_did = part2.charge_cost_centre_did and
> part1.budget_date_did = part2.budget_date_did and
> part1.initiative_did = part2.initiative_did
> EXEC @.Error = EDW_Manager.adm.log_Step_Results @.StepID, @.@.ERROR,
> @.@.ROWCOUNT
> IF (@.Error <> 0) GOTO LBL_Abort|||Consider using temporary tables instead of table variables. The options to
create indexes on table variables are very limited.
For better help, please post DDL, sample data and expected results.
ML
http://milambda.blogspot.com/|||Only a minor point (not performance related), but
you can use a single "coalesce" instead of multiple
nested "isnull"s. This may make your query easier to
read.

Friday, March 23, 2012

Outer Join and WHERE clause strange behavior

I am writing a stored procedure to get aggregrate data out of a web site log
table by joining it with a view
DDL for the table:
CREATE TABLE w3cexlog (
pacifictime datetime,
[cs-method] varchar(20),
target varchar(8000),
query varchar(2000),
username varchar(100),
browser varchar(8000),
referrer varchar(2000),
status int,
substatus int,
win32status int,
[sc-bytes] bigint,
[cs-bytes] bigint,
[time-taken] bigint
)
The view is through ADSI, and pulls the Active Directory OU, samAccountName
(which corresponds to username), and full name for the user.
If the view where a base table, it's DDL would look like this
CREATE TABLE PortalUsers (
name nvarchar(512),
samAccountName nvarchar(512),
OU nvarchar(8000)
)
I want to find out how many hits where generated by users in each OU, along
with visitors from each OU, by joining PortalUsers to w3cexlog on
PortalUsers.samAccountName = w3cexlog.username. (The web site uses AD
authentication, so they must match).
Here is the query:
SELECT p.ou, count(w.username) AS hits, count(distinct w.username) AS visito
rs
FROM portalusers p LEFT JOIN w3cexlog w on
p.samaccountname=w.username
GROUP BY p.ou
ORDER BY 2 DESC, 1
This query gives the expected results; OUs for which none of its users
produced a "hit" still show in the output, but with zero's in the "hits" and
"visitors" columns. However, if I add a WHERE clause to query a specific dat
e
range:
SELECT p.ou, count(w.username) AS hits, count(distinct w.username) AS visito
rs
FROM portalusers p LEFT JOIN w3cexlog w on
p.samaccountname=w.username
WERE w.pacifictime BETWEEN '20060102' AND '20060112'
GROUP BY p.ou
ORDER BY 2 DESC, 1
The OUs that produces no hits are now gone from the result set! That WHERE
clause wouldn't have removed any rows for the OUs that didn't have hits; the
y
where never there to begin with; so why does the behavior of the outer join
change?
In order to get around this, I constructed the following query
SELECT t1.OU, t2.hits, t2.visitors FROM
(
SELECT DISTINCT OU FROM PortalUsers
) t1
LEFT JOIN
(
SELECT p.ou, count(w.username) AS hits, count(distinct w.username) AS visito
rs
FROM portalusers p LEFT JOIN w3cexlog w on
p.samaccountname=w.username
WERE w.pacifictime BETWEEN '20060102' AND '20060112'
GROUP BY p.ou
) t2
ON t1.OU = t2.OU
ORDER BY 2 DESC, 1
But the performance of this is terrible."Mark Williams" <MarkWilliams@.discussions.microsoft.com> wrote in message
news:5002E6BF-4C4F-48B1-B333-D7B398C42430@.microsoft.com...
>I am writing a stored procedure to get aggregrate data out of a web site
>log
> table by joining it with a view
>
> SELECT p.ou, count(w.username) AS hits, count(distinct w.username) AS
> visitors
> FROM portalusers p LEFT JOIN w3cexlog w on
> p.samaccountname=w.username
> WERE w.pacifictime BETWEEN '20060102' AND '20060112'
> GROUP BY p.ou
> ORDER BY 2 DESC, 1
> The OUs that produces no hits are now gone from the result set! That WHERE
> clause wouldn't have removed any rows for the OUs that didn't have hits;
> they
> where never there to begin with; so why does the behavior of the outer
> join
> change?
>
Mark,
This is the way things work. An outer join may contain results
that fail the ON condition, but it will never contain results that fail
the WHERE condition. The WHERE condition is a real filter
than can be thought of as being applied to the result of the
outer join.
If you have result rows with 0 hits, all the result columns from
w3cexlog in those result rows contain NULL. NULL will never
be between '20060102' and '20060112', so you'll see no rows
that failed the ON condition.
Maybe what you want is just
... on p.samaccountname=w.username
AND w.pacifictime BETWEEN '20060102' AND '20060112'
...
Steve Kass
Drew University|||Another option is to leave the join unchanged and alter the where caluse lik
e
this:
...
WHERE (w.pacifictime BETWEEN '20060102' AND '20060112' or w.pacifictime is
null)
...
ML
http://milambda.blogspot.com/|||Thanks Steve and ML. I should have seen it, but I was a little tired! I had
the incorrect thinking that the WHERE clause was applied before the join,
instead of after.
"Steve Kass" wrote:

> "Mark Williams" <MarkWilliams@.discussions.microsoft.com> wrote in message
> news:5002E6BF-4C4F-48B1-B333-D7B398C42430@.microsoft.com...
> Mark,
> This is the way things work. An outer join may contain results
> that fail the ON condition, but it will never contain results that fail
> the WHERE condition. The WHERE condition is a real filter
> than can be thought of as being applied to the result of the
> outer join.
> If you have result rows with 0 hits, all the result columns from
> w3cexlog in those result rows contain NULL. NULL will never
> be between '20060102' and '20060112', so you'll see no rows
> that failed the ON condition.
> Maybe what you want is just
> ... on p.samaccountname=w.username
> AND w.pacifictime BETWEEN '20060102' AND '20060112'
> ...
> Steve Kass
> Drew University
>
>|||It will work, but I wouldn't suggest this as a good option, I'm afraid.
This is like adding "and I really meant that is was an outer join" to the
where clause.
Or did you see some advantage with putting it in the where clause, like some
problem that doing it this way might solve?
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"ML" <ML@.discussions.microsoft.com> wrote in message
news:3F48CD19-D89E-4535-8FFD-0045DF823CA1@.microsoft.com...
> Another option is to leave the join unchanged and alter the where caluse
> like
> this:
> ...
> WHERE (w.pacifictime BETWEEN '20060102' AND '20060112' or w.pacifictime is
> null)
> ...
>
> ML
> --
> http://milambda.blogspot.com/|||Another option is to move the condition to the ON clause:
SELECT t1.OU, t2.hits, t2.visitors FROM
(
SELECT DISTINCT OU FROM PortalUsers
) t1
LEFT JOIN
(
SELECT p.ou, count(w.username) AS hits, count(distinct w.username) AS
visitors, pacifictime
FROM portalusers p LEFT JOIN w3cexlog w on
p.samaccountname=w.username
GROUP BY p.ou
) t2
ON (t1.OU = t2.OU )
AND t2.pacifictime BETWEEN '20060102' AND '20060112'
ORDER BY 2 DESC, 1
That might perform better|||On Sat, 14 Jan 2006 04:46:01 -0800, ML wrote:

>Another option is to leave the join unchanged and alter the where caluse li
ke
>this:
>...
>WHERE (w.pacifictime BETWEEN '20060102' AND '20060112' or w.pacifictime is
>null)
>...
Hi ML,
That would also include rows with w.pacifictime equal to NULL in the
base table (assuming the column is nullable); I don't think that this is
what Mark wants.
Hugo Kornelis, SQL Server MVP|||Since both methods yield same results the only argument would be performance
.
So, which one would perform better?
ML
http://milambda.blogspot.com/|||On Sun, 15 Jan 2006 14:09:01 -0800, ML wrote:

>Since both methods yield same results the only argument would be performanc
e.
>So, which one would perform better?
Hi ML,
Have you testeed them?
(I'll gladly admit I didn't - but due to the OR in your method, I'd be
surprised if it turns out to be the faster version).
Of course, they don't yield the same results if the column is nullible.
Hugo Kornelis, SQL Server MVP|||Beyond what was discussed about how if the pacifictime column was nullable
(you would want to check for the join columns being null) the argument is
style and symantics.
I would argue that it is very important to write readable queries that can
be understood by the next reader AND that make sense. Performance is
important, and if the less clear version peformed measurably better (if
anything it would likely be no better than equal, but sometimes there can be
reasons to do things that don't seem kosher for performance sake, but this
is unlikely to be one of those cases.
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"ML" <ML@.discussions.microsoft.com> wrote in message
news:61118E3B-FAED-4124-86F5-26219564531A@.microsoft.com...
> Since both methods yield same results the only argument would be
> performance.
> So, which one would perform better?
>
> ML
> --
> http://milambda.blogspot.com/

Out put of Select Statement Stored in a variable to another Variab

Hi Guys,
I need a solution in SQL Server 2000. I wanted to store the output of a
select query into a variable. The Select Statement is stored in a variable
.
the sample is given below.
declare @.Tmp Varchar(100),
@.Tmp1 Varchar(100),
@.Tmp2 Varchar(15)
set @.Tmp = 'columntitle from bworksheetdtl where Worksheetcode=1 and
serialno=5'
set @.Tmp1 = 'Select @.Tmp2 = ' + @.Tmp
exec(@.Tmp1)
print @.Tmp2
Do any one have the idea.
Thanks in advance
Regds
Kag"Kag" <Kag@.discussions.microsoft.com> wrote in message
news:304B2B5C-AE55-491A-9D26-88F205861757@.microsoft.com...
> Hi Guys,
> declare @.Tmp Varchar(100),
> @.Tmp1 Varchar(100),
> @.Tmp2 Varchar(15)
> set @.Tmp = 'columntitle from bworksheetdtl where Worksheetcode=1 and
> serialno=5'
> set @.Tmp1 = 'Select @.Tmp2 = ' + @.Tmp
> exec(@.Tmp1)
> print @.Tmp2
>
You will need to use a Temp table. The variables are only valid within the
scope in which they are defined. Temp tables are part of your session.
DECLARE @.tmp varchar(100),
@.tmp1 varchar(100)
CREATE TABLE #ReturnValue (tmp2 varchar(15) NULL)
SET @.tmp = 'columntitle FROM bworksheetdtl WHERE Worksheetcode=1 AND
serialno = 5'
SET @.tmp1 = 'INSERT #ReturnValue (tmp2) SELECT ' + @.tmp
EXECUTE (@.tmp1)
SELECT tmp2 FROM #ReturnValue
DROP TABLE #ReturnValue
Rick Sawtell
MCT, MCSD, MCDBA|||The @.Temp2 variable does not exist (is not declared) inside the context of
your EXECUTE statement.
Take a look at sp_executesql in Books Online. It accepts parameters and
really simplifies execution of dynamic SQL.
On the other hand, if you post DDL and sample data and specify what you're
actually trying to achieve, we can help you find a more efficient solution.
ML|||Hi Kag,
Would you like to try this?
DECLARE @.Tmp Varchar(100),
@.Tmp1 Varchar(100),
@.Tmp2 Varchar(15)
SELECT @.tmp2 = columntitle FROM bworksheetdtl WHERE Worksheetcode = 1 and
serialno = 5
Leo
"Kag" wrote:

> Hi Guys,
> I need a solution in SQL Server 2000. I wanted to store the output of a
> select query into a variable. The Select Statement is stored in a variab
le.
> the sample is given below.
> declare @.Tmp Varchar(100),
> @.Tmp1 Varchar(100),
> @.Tmp2 Varchar(15)
> set @.Tmp = 'columntitle from bworksheetdtl where Worksheetcode=1 and
> serialno=5'
> set @.Tmp1 = 'Select @.Tmp2 = ' + @.Tmp
> exec(@.Tmp1)
> print @.Tmp2
> Do any one have the idea.
> Thanks in advance
> Regds
> Kag|||Hi Kag,
This may be what you want:
Declare @.tmp varchar(15)
SELECT @.temp = columntitle FROM bworksheetdtl WHERE Worksheetcode=1 and
serialno=5
PRINT @.tmp
Leo
"Kag" wrote:

> Hi Guys,
> I need a solution in SQL Server 2000. I wanted to store the output of a
> select query into a variable. The Select Statement is stored in a variab
le.
> the sample is given below.
> declare @.Tmp Varchar(100),
> @.Tmp1 Varchar(100), columntitle from bworksheetdtl where Worksheetcode=1
and
> serialno=5
> @.Tmp2 Varchar(15)
> set @.Tmp = ''
> set @.Tmp1 = 'Select @.Tmp2 = ' + @.Tmp
> exec(@.Tmp1)
> print @.Tmp2
> Do any one have the idea.
> Thanks in advance
> Regds
> Kag|||Pl try code below
declare @.Tmp nVarchar(100), -- varchar changed to nvarchar
@.Tmp1 nVarchar(100), -- varchar changed to nvarchar
@.Tmp2 Varchar(15)
set @.Tmp = 'columntitle from bworksheetdtl where Worksheetcode=1 and
serialno=5'
set @.Tmp1 = 'Select @.Tmp2 = ' + @.Tmp
exec sp_executesql @.Tmp1, N'@.Tmp2 Varchar(15) output', @.Tmp2 output -- alter
ed
print @.Tmp2
Rakesh
"Kag" wrote:

> Hi Guys,
> I need a solution in SQL Server 2000. I wanted to store the output of a
> select query into a variable. The Select Statement is stored in a variab
le.
> the sample is given below.
> declare @.Tmp Varchar(100),
> @.Tmp1 Varchar(100),
> @.Tmp2 Varchar(15)
> set @.Tmp = 'columntitle from bworksheetdtl where Worksheetcode=1 and
> serialno=5'
> set @.Tmp1 = 'Select @.Tmp2 = ' + @.Tmp
> exec(@.Tmp1)
> print @.Tmp2
> Do any one have the idea.
> Thanks in advance
> Regds
> Kag|||Thanks Rakesh and ML. I tried the solution you have given with
sp_executesql and it was the one i was searching for. Special thanks to
Rakesh for the exact example. Thanks again guys..
Kag
"Kag" wrote:

> Hi Guys,
> I need a solution in SQL Server 2000. I wanted to store the output of a
> select query into a variable. The Select Statement is stored in a variab
le.
> the sample is given below.
> declare @.Tmp Varchar(100),
> @.Tmp1 Varchar(100),
> @.Tmp2 Varchar(15)
> set @.Tmp = 'columntitle from bworksheetdtl where Worksheetcode=1 and
> serialno=5'
> set @.Tmp1 = 'Select @.Tmp2 = ' + @.Tmp
> exec(@.Tmp1)
> print @.Tmp2
> Do any one have the idea.
> Thanks in advance
> Regds
> Kagsql

Wednesday, March 21, 2012

Out in email.

I am using extended stored procedure xp_sendmail to send the email now my
question is, is there any way in can write the out of following query into
one file and send it to through email? or output directly goes to email..
restore FILELISTONLY from disk = 'c:\fullpath.bak'
Thanks
Probably a few ways but a couple off the top of my head -
Execute the command using osql and specify an output file in
the osql command.
Execute the command within a job - for the job step doing
the restore filelistonly, go to the advanced tab and specify
an output file.
You can then send the output file as an attachment using
xp_sendmail.
-Sue
On Mon, 30 Oct 2006 08:20:28 -0500, "Rogers"
<naissani@.hotmail.com> wrote:

>I am using extended stored procedure xp_sendmail to send the email now my
>question is, is there any way in can write the out of following query into
>one file and send it to through email? or output directly goes to email..
>restore FILELISTONLY from disk = 'c:\fullpath.bak'
>Thanks
>
|||Thanks Sue...
"Sue Hoegemeier" <Sue_H@.nomail.please> wrote in message
news:0ladk2d0g0n0gm69qv3ot2tgq5fagd7vkr@.4ax.com...
> Probably a few ways but a couple off the top of my head -
> Execute the command using osql and specify an output file in
> the osql command.
> Execute the command within a job - for the job step doing
> the restore filelistonly, go to the advanced tab and specify
> an output file.
> You can then send the output file as an attachment using
> xp_sendmail.
> -Sue
> On Mon, 30 Oct 2006 08:20:28 -0500, "Rogers"
> <naissani@.hotmail.com> wrote:
>

OUT and OUTPUT - Stored procedure parameters.

Hi All,

The following is a code snippit. My main interests are the OUT and OUTPUT parameter keywords. One returns a single value, and the other seemingly a resultset. OUTPUT returns a single value, however OUT seems to return a list of values. Could I please get this confirmed?

Also, I cannot see how the value being returned by OUT is being iterated...

Any help on the obove two matters is appreciated.

Thank You

Chris

BEGIN SNIPPET-

--The following example creates the Production.usp_GetList

--stored procedure, which returns a list of products that have

--prices that do not exceed a specified amount.

USE AdventureWorks;

GO

IF OBJECT_ID ( 'Production.uspGetList', 'P' ) IS NOT NULL

DROP PROCEDURE Production.uspGetList;

GO

CREATE PROCEDURE Production.uspGetList @.Product varchar(40)

, @.MaxPrice money

, @.ComparePrice money OUTPUT

, @.ListPrice money OUT

AS

SELECT p.[Name] AS Product, p.ListPrice AS 'List Price'

FROM Production.Product AS p

JOIN Production.ProductSubcategory AS s

ON p.ProductSubcategoryID = s.ProductSubcategoryID

WHERE s.[Name] LIKE @.Product AND p.ListPrice < @.MaxPrice;

-- Populate the output variable @.ListPprice.

SET @.ListPrice = (SELECT MAX(p.ListPrice)

FROM Production.Product AS p

JOIN Production.ProductSubcategory AS s

ON p.ProductSubcategoryID = s.ProductSubcategoryID

WHERE s.[Name] LIKE @.Product AND p.ListPrice < @.MaxPrice);

-- Populate the output variable @.compareprice.

SET @.ComparePrice = @.MaxPrice;

GO

USE

DECLARE @.ComparePrice money, @.Cost money

EXECUTE Production.uspGetList '%Bikes%', 700,

@.ComparePrice OUT,

@.Cost OUTPUT

IF @.Cost <= @.ComparePrice

BEGIN

PRINT 'These products can be purchased for less than

$'+RTRIM(CAST(@.ComparePrice AS varchar(20)))+'.'

END

ELSE

PRINT 'The prices for all products in this category exceed

$'+ RTRIM(CAST(@.ComparePrice AS varchar(20)))+'.'

-

Partial Result Set

-

--Product List Price

-

--Road-750 Black, 58 539.99

--Mountain-500 Silver, 40 564.99

--Mountain-500 Silver, 42 564.99

--...

--Road-750 Black, 48 539.99

--Road-750 Black, 52 539.99

--

--(14 row(s) affected)

--

--These items can be purchased for less than $700.00.

Well, OUT and OUTPUT keywords are synonyms, just as INT and INTEGER are.|||

Really?

AAAAAAAARGH it's all clear now. I can't see why in the example they mix and match however though - it just brings confusion into the equation.

No point in answering the other part of my Q: I 'm sure the example iterates throught the execution of the SP, but doesn't show it.

Thank you Sergey.

Tuesday, March 20, 2012

other possibilities than cursor

I've written a set of stored procedures that currently use a cursor to pull
data from a staging table into two separate tables. The two destination
tables are linked by a Foreign Key relationship on the primary key (can't
change this design), which is why I need to use a cursor to be able to inser
t
records into both tables.
I was wondering whether there is any other possible solution to using a
cursor to insert records into the two tables?Another thing I forgot to mention is that I'm generating new
uniqueidentifiers within the cursor for each new record as primary keys for
the inserted records (there are no primary keys within the staging table)
such that destination_table1.accountId = destination_table2.accountId
"EL" wrote:

> I've written a set of stored procedures that currently use a cursor to pul
l
> data from a staging table into two separate tables. The two destination
> tables are linked by a Foreign Key relationship on the primary key (can't
> change this design), which is why I need to use a cursor to be able to ins
ert
> records into both tables.
> I was wondering whether there is any other possible solution to using a
> cursor to insert records into the two tables?|||Can you give the columns in the input tables
and what all needs to go to the first and second table?
--
"EL" wrote:
> Another thing I forgot to mention is that I'm generating new
> uniqueidentifiers within the cursor for each new record as primary keys fo
r
> the inserted records (there are no primary keys within the staging table)
> such that destination_table1.accountId = destination_table2.accountId
> "EL" wrote:
>|||Can you post CREATE TABLE statements, and a couple of lines of sample
data; that would be helpful for those of us who want to copy and paste
the code into our own query analyzers to test. Just off the top of my
head, can you not use temp tables or table variables to pre-generate
the uniqueidentifiers on the parent and child tables, and then insert
the parent rows followed by the child rows?
Without trying to open up a whole can of worms on the issue of
surrogate vs natural keys, why are you using uniqueidentifiers?
Although there are times when the uniqueidentifier is necessary, it has
some consequences.
Stu|||Thanks for youre reply omnibuzz.
I also forgot to mention that I would be inserting into 3 separate tables.
I'll explain them later in this message.
Here's a subset of the columns that exist in the single staging table:
TTC_Agency_Id int null,
Agency_Name varchar(150) NULL,
Agency_Addr1 varchar(150) NULL,
Agency_Addr2 varchar(150) NULL,
Agency_Addr3 varchar(150) NULL,
Agency_City varchar(150) NULL,
Agency_State varchar(150) NULL,
Agency_Zip varchar(150) NULL,
Agency_Country varchar (150) NULL,
Agency_IATA_Number varchar(25) NULL,
Agency_Email varchar(100) NULL,
Agency_Barred varchar(10) NULL,
Agency_Fax varchar (50) NULL,
Agency_Phone varchar(50) NULL
This staging table contains various other columns used by other stored
procedures which are more or less similar in their function. If I was
currently running the procedure that pulls all the data contained in the
Agency_... columns from the staging table then the procedure would be doing
the following (pseudo code):
create cursor from query that pulls Agency_... column data from staging tabl
e
Within Cursor:
Create uniqueidentifier value (Primary Key) -- call this newid
Create uniqueidentifier value (Address Primary Key) -- call this
addressId
Insert into table1 values (newid, Agency_Name, Agency_IATA,
ttc_agency_id)
Insert into table2 values (newid, Agency_Email, Agency_Barred,
Agency_Fax, Agency_Phone)
Insert into table3 values(addressId, newid, Agency_Addr1,
Agency_Addr2, Agency_Addr3, Agency_City, Agency_State, Agency_Zip,
Agency_Country)
close cursor
Table1 and table2 are the main destination tables with a foreign key
relationship on the primary keys (table1.accountId = table2.accountId) which
is why I create a new id for the agency inside the cursor.
Table3 is another table that holds address details (in this case holds the
agency address details)
I thought about putting a uniqueidetifier column within my source / staging
table that gets populated when data is loading into that table and use that
value for the primary keys of the destination tables (table1 and table2), bu
t
that would mean that I would no longer be able to perform bulk inserts into
that table.
"Omnibuzz" wrote:
> Can you give the columns in the input tables
> and what all needs to go to the first and second table?
> --
>
>
> "EL" wrote:
>|||Here's the script for the staging table that I use (simplified down)
Create table TempBookings (
Agency_Name varchar(150) NULL,
Agency_Addr1 varchar(150) NULL,
Agency_Addr2 varchar(150) NULL,
Agency_Addr3 varchar(150) NULL,
Agency_City varchar(150) NULL,
Agency_State varchar(150) NULL,
Agency_Zip varchar(150) NULL,
Agency_Country varchar (150) NULL,
Agency_IATA_Number varchar(25) NULL,
Agency_Email varchar(100) NULL,
Agency_Barred varchar(10) NULL,
Agency_Fax varchar (50) NULL,
Agency_Phone varchar(50) NULL,
Agency_Code varchar (30) NULL,
TTC_Agency_Id int NULL
)
Here's some insert sql code for inserting values into the staging table
insert into TempBookings values ('Test 1', 'Test_1_Addr1', 'Test_1_Addr2',
'Test_1_Addr3', 'Test_1_City', 'Test_1_State', 'Test_1_Zip',
'Test_1_Country', 'Test_1_IATA', 'Test_1_Email@.Test.com', '1', '12345678',
'12345689', 'Test_1_Code', 50)
insert into TempBookings values ('Test 2', 'Test_2_Addr1', 'Test_2_Addr2',
'Test_2_Addr3', 'Test_2_City', 'Test_2_State', 'Test_2_Zip',
'Test_2_Country', 'Test_2_IATA', 'Test_2_Email@.Test.com', '1', '12345678',
'12345689', 'Test_2_Code', 52)
insert into TempBookings values ('Test 3', 'Test_3_Addr1', 'Test_3_Addr2',
'Test_3_Addr3', 'Test_3_City', 'Test_3_State', 'Test_3_Zip',
'Test_3_Country', 'Test_3_IATA', 'Test_3_Email@.Test.com', '1', '12345678',
'12345689', 'Test_3_Code', 65)
insert into TempBookings values ('Test 4', 'Test_4_Addr1', 'Test_4_Addr2',
'Test_4_Addr3', 'Test_4_City', 'Test_4_State', 'Test_4_Zip',
'Test_4_Country', 'Test_4_IATA', 'Test_4_Email@.Test.com', '1', '12345678',
'12345689', 'Test_4_Code', 67)
insert into TempBookings values ('Test 5', 'Test_5_Addr1', 'Test_5_Addr2',
'Test_5_Addr3', 'Test_5_City', 'Test_5_State', 'Test_5_Zip',
'Test_5_Country', 'Test_5_IATA', 'Test_5_Email@.Test.com', '1', '12345678',
'12345689', 'Test_5_Code', 68)
insert into TempBookings values ('Test 6', 'Test_6_Addr1', 'Test_6_Addr2',
'Test_6_Addr3', 'Test_6_City', 'Test_6_State', 'Test_6_Zip',
'Test_6_Country', 'Test_6_IATA', 'Test_6_Email@.Test.com', '1', '12345678',
'12345689', 'Test_6_Code', 69)
insert into TempBookings values ('Test 7', 'Test_7_Addr1', 'Test_7_Addr2',
'Test_7_Addr3', 'Test_7_City', 'Test_7_State', 'Test_7_Zip',
'Test_7_Country', 'Test_7_IATA', 'Test_7_Email@.Test.com', '1', '12345678',
'12345689', 'Test_7_Code', 70)
insert into TempBookings values ('Test 8', 'Test_8_Addr1', 'Test_8_Addr2',
'Test_8_Addr3', 'Test_8_City', 'Test_8_State', 'Test_8_Zip',
'Test_8_Country', 'Test_8_IATA', 'Test_8_Email@.Test.com', '1', '12345678',
'12345689', 'Test_8_Code', 71)
insert into TempBookings values ('Test 9', 'Test_9_Addr1', 'Test_9_Addr2',
'Test_9_Addr3', 'Test_9_City', 'Test_9_State', 'Test_9_Zip',
'Test_9_Country', 'Test_9_IATA', 'Test_9_Email@.Test.com', '1', '12345678',
'12345689', 'Test_9_Code', 72)
insert into TempBookings values ('Test 10', 'Test_10_Addr1',
'Test_10_Addr2', 'Test_10_Addr3', 'Test_10_City', 'Test_10_State',
'Test_10_Zip', 'Test_10_Country', 'Test_10_IATA', 'Test_10_Email@.Test.com',
'1', '12345678', '12345689', 'Test_10_Code', 73)
The scripts below have been generated by scripting the destination tables
involved in one of the storedprocedures
CREATE TABLE [dbo].[AccountBase] (
[AccountId] uniqueidentifier ROWGUIDCOL NOT NULL ,
[Name] [nvarchar] (160) COLLATE Latin1_General_CI_AS NULL ,
[AccountNumber] [nvarchar] (20) COLLATE Latin1_General_CI_AS NULL ,
[EMailAddress1] [nvarchar] (100) COLLATE Latin1_General_CI_AS NULL ,
[Telephone1] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[Fax] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[AccountExtensionBase] (
[AccountId] [uniqueidentifier] NOT NULL ,
[New_AccountStatus] [int] NULL ,
[New_TTC_Id] [int] NULL ,
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[AccountBase] WITH NOCHECK ADD
CONSTRAINT [cndx_PrimaryKey_Account] PRIMARY KEY CLUSTERED
(
[AccountId]
) WITH FILLFACTOR = 80 ON [PRIMARY]
GO
ALTER TABLE [dbo].[AccountExtensionBase] WITH NOCHECK ADD
CONSTRAINT [PK_AccountExtensionBase] PRIMARY KEY CLUSTERED
(
[AccountId]
) WITH FILLFACTOR = 80 ON [PRIMARY]
GO
CREATE INDEX [ndx_for_cascaderelationship_account_par
ent_account] ON
[dbo].[AccountBase]([ParentAccountId]) WITH FILLFACTOR = 80 ON [PRIMARY]
GO
CREATE TABLE [dbo].[CustomerAddressBase] (
[ParentId] [uniqueidentifier] NOT NULL ,
[CustomerAddressId] uniqueidentifier ROWGUIDCOL NOT NULL ,
[AddressNumber] [int] NULL ,
[Name] [nvarchar] (200) COLLATE Latin1_General_CI_AS NULL ,
[Line1] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[Line2] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[Line3] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[City] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[StateOrProvince] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[Country] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[PostalCode] [nvarchar] (20) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerAddressBase] WITH NOCHECK ADD
CONSTRAINT [cndx_PrimaryKey_CustomerAddress] PRIMARY KEY CLUSTERED
(
[CustomerAddressId]
) WITH FILLFACTOR = 80 ON [PRIMARY]
GO
CREATE INDEX [ndx_for_cascaderelationship_Contact_Cus
tomerAddress] ON
[dbo].[CustomerAddressBase]([ParentId]) WITH FILLFACTOR = 80 ON [PRIMARY]
GO
"Stu" wrote:

> Can you post CREATE TABLE statements, and a couple of lines of sample
> data; that would be helpful for those of us who want to copy and paste
> the code into our own query analyzers to test. Just off the top of my
> head, can you not use temp tables or table variables to pre-generate
> the uniqueidentifiers on the parent and child tables, and then insert
> the parent rows followed by the child rows?
> Without trying to open up a whole can of worms on the issue of
> surrogate vs natural keys, why are you using uniqueidentifiers?
> Although there are times when the uniqueidentifier is necessary, it has
> some consequences.
> Stu
>|||Here's my stored procedure (rather the cursor) that I use to insert the
records into the destination tables from the staging table. I've simplified
it down quite heavily just for this example, and so it may not work correctl
y
declare @.Name varchar(150)
declare @.AddressLine1 varchar(50)
declare @.AddressLine2 varchar(50)
declare @.AddressLine3 varchar(50)
declare @.City varchar(50)
declare @.State varchar(50)
declare @.Zip varchar(50)
declare @.Country varchar(50)
declare @.IATA varchar(30)
declare @.AgencyEmail varchar(100)
declare @.New_AccountStatusNew varchar(5)
declare @.NewId uniqueidentifier
declare @.AddressId uniqueidentifier
declare TravelAgenciesCursor Cursor LOCAL FAST_FORWARD for
select distinct Agency_Name, Agency_Addr1, Agency_Addr2, Agency_addr3,
Agency_City, Agency_State, Agency_Zip, Agency_Country,
Agency_IATA_Number, Agency_Email, Agency_Barred Agency_Fax, TTC_Agency_Id,
Agency_Phone
from TempBooking
open TravelAgenciesCursor
Fetch next from TravelAgenciesCursor into @.Name, @.AddressLine1,
@.AddressLine2, @.AddressLine3, @.City,
@.State, @.Zip, @.Country, @.IATA, @.AgencyEmail, @.New_AccountStatusNew,
@.AgencyFax, @.TTC_Id, @.Phone
While (@.@.Fetch_Status <> -1)
Begin
if (@.@.Fetch_Status <> -2)
Begin
-- Set a new Consortium Id every time new insert and get agency type value
and the current date
set @.NewId = NewId()
-- Inserts sets for inserting Agencies
Insert into AccountBase (AccountId, Name, AccountNumber, EMailAddress1,
Telephone1, Fax) values
(@.NewId, @.Name, @.IATA, @.AgencyEmail, @.Phone, @.Fax)
insert into AccountExtensionBase (AccountId, New_AccountStatus,
New_TTC_Id) values
(@.NewId, 1, @.TTC_Id)
-- insert a new record for the customer
insert into CustomerAddressBase (ParentId, CustomerAddressId,
AddressNumber, Name, Line1, Line2, Line3,
City, StateOrProvince, Country, PostalCode) values
(@.NewId, NewId(), 1, @.Name, @.AddressLine1, @.AddressLine2, @.AddressLine3,
@.City, @.State, @.Country, @.Zip)
End
Fetch next from TravelAgenciesCursor into @.Name, @.AddressLine1,
@.AddressLine2, @.AddressLine3, @.City,
@.State, @.Zip, @.Country, @.IATA, @.AgencyEmail, @.New_AccountStatusNew,
@.AgencyFax, @.TTC_Id, @.Phone
End
Close TravelAgenciesCursor
deallocate TravelAgenciesCursor
As regards the use of the temp tables of table variables, I did write a
version of the same stored procedure using a table variable instead of a
cursor, but in most of the cases the query optimizer either showed either
very little or no improvement or worse query performance using the table
variable.
I'm guessing the use of a uniqueidentifier as the primary key was to
maintain global uniqueness for each record, which in this situation isn't
really necessary. I would have wanted to use an int value instead but making
any changes to the table or database design is out of the question.
"EL" wrote:
> Here's the script for the staging table that I use (simplified down)
> Create table TempBookings (
> Agency_Name varchar(150) NULL,
> Agency_Addr1 varchar(150) NULL,
> Agency_Addr2 varchar(150) NULL,
> Agency_Addr3 varchar(150) NULL,
> Agency_City varchar(150) NULL,
> Agency_State varchar(150) NULL,
> Agency_Zip varchar(150) NULL,
> Agency_Country varchar (150) NULL,
> Agency_IATA_Number varchar(25) NULL,
> Agency_Email varchar(100) NULL,
> Agency_Barred varchar(10) NULL,
> Agency_Fax varchar (50) NULL,
> Agency_Phone varchar(50) NULL,
> Agency_Code varchar (30) NULL,
> TTC_Agency_Id int NULL
> )
> Here's some insert sql code for inserting values into the staging table
> insert into TempBookings values ('Test 1', 'Test_1_Addr1', 'Test_1_Addr2',
> 'Test_1_Addr3', 'Test_1_City', 'Test_1_State', 'Test_1_Zip',
> 'Test_1_Country', 'Test_1_IATA', 'Test_1_Email@.Test.com', '1', '12345678',
> '12345689', 'Test_1_Code', 50)
> insert into TempBookings values ('Test 2', 'Test_2_Addr1', 'Test_2_Addr2',
> 'Test_2_Addr3', 'Test_2_City', 'Test_2_State', 'Test_2_Zip',
> 'Test_2_Country', 'Test_2_IATA', 'Test_2_Email@.Test.com', '1', '12345678',
> '12345689', 'Test_2_Code', 52)
> insert into TempBookings values ('Test 3', 'Test_3_Addr1', 'Test_3_Addr2',
> 'Test_3_Addr3', 'Test_3_City', 'Test_3_State', 'Test_3_Zip',
> 'Test_3_Country', 'Test_3_IATA', 'Test_3_Email@.Test.com', '1', '12345678',
> '12345689', 'Test_3_Code', 65)
> insert into TempBookings values ('Test 4', 'Test_4_Addr1', 'Test_4_Addr2',
> 'Test_4_Addr3', 'Test_4_City', 'Test_4_State', 'Test_4_Zip',
> 'Test_4_Country', 'Test_4_IATA', 'Test_4_Email@.Test.com', '1', '12345678',
> '12345689', 'Test_4_Code', 67)
> insert into TempBookings values ('Test 5', 'Test_5_Addr1', 'Test_5_Addr2',
> 'Test_5_Addr3', 'Test_5_City', 'Test_5_State', 'Test_5_Zip',
> 'Test_5_Country', 'Test_5_IATA', 'Test_5_Email@.Test.com', '1', '12345678',
> '12345689', 'Test_5_Code', 68)
> insert into TempBookings values ('Test 6', 'Test_6_Addr1', 'Test_6_Addr2',
> 'Test_6_Addr3', 'Test_6_City', 'Test_6_State', 'Test_6_Zip',
> 'Test_6_Country', 'Test_6_IATA', 'Test_6_Email@.Test.com', '1', '12345678',
> '12345689', 'Test_6_Code', 69)
> insert into TempBookings values ('Test 7', 'Test_7_Addr1', 'Test_7_Addr2',
> 'Test_7_Addr3', 'Test_7_City', 'Test_7_State', 'Test_7_Zip',
> 'Test_7_Country', 'Test_7_IATA', 'Test_7_Email@.Test.com', '1', '12345678',
> '12345689', 'Test_7_Code', 70)
> insert into TempBookings values ('Test 8', 'Test_8_Addr1', 'Test_8_Addr2',
> 'Test_8_Addr3', 'Test_8_City', 'Test_8_State', 'Test_8_Zip',
> 'Test_8_Country', 'Test_8_IATA', 'Test_8_Email@.Test.com', '1', '12345678',
> '12345689', 'Test_8_Code', 71)
> insert into TempBookings values ('Test 9', 'Test_9_Addr1', 'Test_9_Addr2',
> 'Test_9_Addr3', 'Test_9_City', 'Test_9_State', 'Test_9_Zip',
> 'Test_9_Country', 'Test_9_IATA', 'Test_9_Email@.Test.com', '1', '12345678',
> '12345689', 'Test_9_Code', 72)
> insert into TempBookings values ('Test 10', 'Test_10_Addr1',
> 'Test_10_Addr2', 'Test_10_Addr3', 'Test_10_City', 'Test_10_State',
> 'Test_10_Zip', 'Test_10_Country', 'Test_10_IATA', 'Test_10_Email@.Test.com'
,
> '1', '12345678', '12345689', 'Test_10_Code', 73)
>
> The scripts below have been generated by scripting the destination tables
> involved in one of the storedprocedures
> CREATE TABLE [dbo].[AccountBase] (
> [AccountId] uniqueidentifier ROWGUIDCOL NOT NULL ,
> [Name] [nvarchar] (160) COLLATE Latin1_General_CI_AS NULL ,
> [AccountNumber] [nvarchar] (20) COLLATE Latin1_General_CI_AS NULL ,
> [EMailAddress1] [nvarchar] (100) COLLATE Latin1_General_CI_AS NULL ,
> [Telephone1] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
> [Fax] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
> ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[AccountExtensionBase] (
> [AccountId] [uniqueidentifier] NOT NULL ,
> [New_AccountStatus] [int] NULL ,
> [New_TTC_Id] [int] NULL ,
> ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[AccountBase] WITH NOCHECK ADD
> CONSTRAINT [cndx_PrimaryKey_Account] PRIMARY KEY CLUSTERED
> (
> [AccountId]
> ) WITH FILLFACTOR = 80 ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[AccountExtensionBase] WITH NOCHECK ADD
> CONSTRAINT [PK_AccountExtensionBase] PRIMARY KEY CLUSTERED
> (
> [AccountId]
> ) WITH FILLFACTOR = 80 ON [PRIMARY]
> GO
> CREATE INDEX [ndx_for_cascaderelationship_account_par
ent_account] ON
> [dbo].[AccountBase]([ParentAccountId]) WITH FILLFACTOR = 80 ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[CustomerAddressBase] (
> [ParentId] [uniqueidentifier] NOT NULL ,
> [CustomerAddressId] uniqueidentifier ROWGUIDCOL NOT NULL ,
> [AddressNumber] [int] NULL ,
> [Name] [nvarchar] (200) COLLATE Latin1_General_CI_AS NULL ,
> [Line1] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
> [Line2] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
> [Line3] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
> [City] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
> [StateOrProvince] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
> [Country] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
> [PostalCode] [nvarchar] (20) COLLATE Latin1_General_CI_AS NULL
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[CustomerAddressBase] WITH NOCHECK ADD
> CONSTRAINT [cndx_PrimaryKey_CustomerAddress] PRIMARY KEY CLUSTERED
> (
> [CustomerAddressId]
> ) WITH FILLFACTOR = 80 ON [PRIMARY]
> GO
>
> CREATE INDEX [ndx_for_cascaderelationship_Contact_Cus
tomerAddress] ON
> [dbo].[CustomerAddressBase]([ParentId]) WITH FILLFACTOR = 80 ON [PRIMARY]
> GO
>
> "Stu" wrote:
>|||The design and your specification doesn't give much hope of preserving
integrity. If you are forced to accept it you may just have to make the
best of a bad job. The following is an example although probably not a
very good one. A good solution depends on whether there is some
uniqueness in the source data that can be preserved even despite the
missing keys. From your sample data I can only guess
(Agency_IATA_Number for example?).
I didn't read your code to identify which columns mapped to which, I
just guessed. Hopefully this will give you some ideas.
What I'd also do is document the fact that the model may make a
nonsense of the data. Unfortunately it's often not until the business
customers suffer that IT departments take note of data quality issues.
INSERT INTO dbo.AccountBase
(AccountId, Name, AccountNumber, EMailAddress1, Telephone1, Fax)
SELECT NEWID(), MAX(Agency_Name), Agency_IATA_Number,
MAX(Agency_Email), MAX(Agency_Phone), MAX(Agency_Fax)
FROM dbo.TempBookings
GROUP BY Agency_IATA_Number ;
INSERT INTO dbo.AccountExtensionBase (AccountId, New_AccountStatus,
New_TTC_Id)
SELECT A.AccountId, 0, MIN(T.TTC_Agency_Id)
FROM dbo.TempBookings AS T
JOIN dbo.AccountBase AS A
ON T.Agency_IATA_Number = A.AccountNumber
GROUP BY A.AccountId ;
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||try this.. hope this helps.
-- setting up the staging table for my example
create table #staging (a int, b int)
insert into #staging values(1,1)
insert into #staging values(1,3)
insert into #staging values(2,1)
insert into #staging values(1,2)
insert into #staging values(2,3)
insert into #staging values(3,3)
-- till here was the setup to the staging table
alter table #staging add c uniqueidentifier not null constraint df1 default
newid()
--do you bulk insert here.. now that you have the guid
-- get your staging table back to its original form
alter table #staging drop constraint df1
alter table #staging drop column c
-- this is for my example :)
drop table #staging|||OK,
So based off your stored procedure, all of your relationships between
entities are 1-to-1. You have two choices; either alter your staging
table AFTER your bulk insert in order to add a uniqueidentifier for
your data, OR move the data from your tempstaging table to a second
staging table with the uniqueidentifier already inserted.
Assuming that you want to do the former (should perform the best), you
can do something like this (sorry for the pseudo-code; running out the
door to work):
CREATE proc...
BEGIN TRANSACTION
ALTER TABLE TempStaging
ADD UNID uniqueidentifier DEFAULT newid WITH VALUES
INSERT INTO table1
SELECT table 1 columns
INSERT INTO table2...
INSERT INTO table3...
ALTER TABLE TempStaging
DROP COLUMN UNID
END TRANSACTION
Maybe that will get you started.
HTH,
Stu

Monday, March 12, 2012

osql versus stored procedural using in a web service

I have many problems with the stored procedural used at web service (and
this′s used by aspx page) because i get a timeout expired or this stored
procedural is very very very slowly (more than 30 minutes). Although if i use
the console and osql this stored procedural runs very very quickly (some
seconds).
Can i do somethings?
Regards, and thank very much,
Hello,
Perheaps you can try with SET FORCEPLAN
to be sure that sql server will take the correct plan
Marc Allard
Allcomp
"fravelgueAThotmailDOTcom"
<fravelgueAThotmailDOTcom@.discussions.microsoft.co m> a crit dans le message
de news: E02F7AAB-7EAE-4CA7-80B1-5A88FB2A85EF@.microsoft.com...
>I have many problems with the stored procedural used at web service (and
> thiss used by aspx page) because i get a timeout expired or this stored
> procedural is very very very slowly (more than 30 minutes). Although if i
> use
> the console and osql this stored procedural runs very very quickly (some
> seconds).
> Can i do somethings?
> Regards, and thank very much,
|||hi,
"Allcomp" <fa097770@.nospam.skynet.be> ha scritto nel messaggio
news:uaPZusetEHA.2300@.TK2MSFTNGP09.phx.gbl
> Hello,
> Perheaps you can try with SET FORCEPLAN
> to be sure that sql server will take the correct plan
SET FORCEPLAN will instuct the query optimizer to execute the join
condition(s) in the (manual) order they are presented by the developer, and
I'd be very carefull in it's use...
as oSql.exe executes the very same query in short time, I'd rather think the
problem should be bound to the web service it self or elsewhere in the
connection...
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.9.1 - DbaMgr ver 0.55.1
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply
|||"Andrea Montanari" wrote:

> hi,
> "Allcomp" <fa097770@.nospam.skynet.be> ha scritto nel messaggio
> news:uaPZusetEHA.2300@.TK2MSFTNGP09.phx.gbl
> SET FORCEPLAN will instuct the query optimizer to execute the join
> condition(s) in the (manual) order they are presented by the developer, and
> I'd be very carefull in it's use...
> as oSql.exe executes the very same query in short time, I'd rather think the
> problem should be bound to the web service it self or elsewhere in the
> connection...
Can you help me more?
I have changed timeout of web.config and and i set the connect timeout,
but i don′t know how i can do it

> --
> Andrea Montanari (Microsoft MVP - SQL Server)
> http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
> DbaMgr2k ver 0.9.1 - DbaMgr ver 0.55.1
> (my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
> interface)
> -- remove DMO to reply
>
|||hi,
"fravelgueAThotmailDOTcom"
> Can you help me more?
> I have changed timeout of web.config and and i set the connect
> timeout, but i don′t know how i can do it
actually not... I'm very bad on web tihgs =;-D
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.9.1 - DbaMgr ver 0.55.1
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply

Saturday, February 25, 2012

Osql and CommandLine stored procedures

I'm currently developing a number of SQL scripts to handle a major upgrade of our database from a development server to a production server using Osql.

Everything is working fine, but I have each of the scripts generating text-output and am hoping someone may know how to suppress some the Caution text (specifically triggered when a column has been renamed).

Can anyone provide a possible solution? (there's so many notes being written to the console its easy to miss the errors I'm concerned about.

Thanks!

TedNot that I am aware of. You might try SET ANSI_WARNINGS OFF but that will not suppress the types of warnings to which you are referring.

Terri

OSQL - appending output to a file

I want to run a job every hour which calls a stored proc using osql and apppends the results to a text file.
But it always overwrites the previous results. Is there a way of getting osql to append the results instead?Originally posted by mwilkes
I want to run a job every hour which calls a stored proc using osql and apppends the results to a text file.

But it always overwrites the previous results. Is there a way of getting osql to append the results instead?

Good old DOS, you should use batch file to append OSQL results into existing text file, named differently than the one used in OSQL output, something like:

osql ... > Osql.Txt

Type Osql.Txt >> FinalResult.Txt

Where FinalResult.Txt contains all, appended results.

Cheers, Marin

os_oa* run exe written in vb6

I need to launch an exe application written in VB6 from stored procedure or
trigger. I know I have to use os_oa* but I cannot find any document to help
me build one. Many examples that I found have something to do with com but I
only have exe. I know this is not the way to go but I was told to do it. Can
any one help me by pointing me in the right directions? I know nothing about
this so I am learning from scratch for os_oa*.
The exe was developed to run silent in the background with no user
interface.Search for xp_cmdshell in BOL, it may be useful.
Francesco Anti
"UGH" <nospam@.noSPam.com> wrote in message
news:eRxL9tFaFHA.1368@.tk2msftngp13.phx.gbl...
>I need to launch an exe application written in VB6 from stored procedure or
>trigger. I know I have to use os_oa* but I cannot find any document to help
>me build one. Many examples that I found have something to do with com but
>I only have exe. I know this is not the way to go but I was told to do it.
>Can any one help me by pointing me in the right directions? I know nothing
>about this so I am learning from scratch for os_oa*.
>
> The exe was developed to run silent in the background with no user
> interface.
>|||Do you mean sp_oa* ' If so, here is everything you need:
1. Compile your VB6 class into a DLL. You better have good error handling in
it otherwise you risk crashing your SQL Server.
2. Place that DLL into the WIN32 directory on your SQL Server, and REGISTER
it on your SQL Server by using RegSvr32.exe
3. Call methods of your VB6 class/dll from any stored procedure, using
something like the following (some of the names have been changed to protect
the innocent):
BEGIN
blah blah blah
DECLARE @.Object int -- holds a reference to your object instantiated
from the vb6 class.
SET @.MethodToCall = 'CalculatePayment(' + @.RatePercent + ', ' +
@.RateIncrease + ', ' + @.Months + ', ' + @.Fees + ')'
--Instantiate an instance of our DataConversion class and put it's
reference in @.object
EXEC sp_OACreate 'Your_VB_Class.DataConversion', @.object OUT
--Run the CalculatePayment method of our DataConversion class - and
place it's output into @.Return
EXEC sp_OAMethod @.object, @.MethodToCall, @.return OUT
-- Destroy the instance of our DataConversion class now that we're
done with it.
EXEC sp_OADestroy @.object
blah blah blah
END
4. Any time you need to upgrade your VB6 DLL, be sure to Unregister the old
one (using RegSvr32.exe... -U), then replace the old DLL with the new one,
and then register the new one like in step 2 above. No need to restart your
SQL Server.
That's it - really quite straight-forward, actually.
-HTH
"UGH" <nospam@.noSPam.com> wrote in message
news:eRxL9tFaFHA.1368@.tk2msftngp13.phx.gbl...
>I need to launch an exe application written in VB6 from stored procedure or
>trigger. I know I have to use os_oa* but I cannot find any document to help
>me build one. Many examples that I found have something to do with com but
>I only have exe. I know this is not the way to go but I was told to do it.
>Can any one help me by pointing me in the right directions? I know nothing
>about this so I am learning from scratch for os_oa*.
>
> The exe was developed to run silent in the background with no user
> interface.
>|||Hi,
Use the following :
EXEC xp_cmdshell 'your vb program.exe'
you want to grant the right to execute xp_cmdshell to the SQL login
LimitedUser.
You'll need an NT account to execute the program. Here's the script:
use master
go
xp_sqlagent_proxy_account N'SET'
, N'<mydomain>'
, N'<ntuser>'
, N'<ntuser's password>'
go
-- retrieve the proxy account to check that it's correct.
xp_sqlagent_proxy_account N'GET'
go
-- grant database access in master
sp_grantdbaccess 'LimitedUser'
go
grant exec on xp_cmdshell to LimitedUser
go
Thanks,
Tarek ghazali
"UGH" <nospam@.noSPam.com> wrote in message
news:eRxL9tFaFHA.1368@.tk2msftngp13.phx.gbl...
>I need to launch an exe application written in VB6 from stored procedure or
>trigger. I know I have to use os_oa* but I cannot find any document to help
>me build one. Many examples that I found have something to do with com but
>I only have exe. I know this is not the way to go but I was told to do it.
>Can any one help me by pointing me in the right directions? I know nothing
>about this so I am learning from scratch for os_oa*.
>
> The exe was developed to run silent in the background with no user
> interface.
>

Monday, February 20, 2012

Orphaned Sessions

Hi,
I have a SQL Agent task that fails, however the session carries on
regradless and compeltes the task successfully. The task is a call to a
stored procedure, and the Agent just gives an error of unknown token.
Running SQL 2000 sp3a on Windows nt4 server.
Can anyone shed any light on this?Hi
Have you checked the log files for the agent and SQL Server?
Does your stored procedure have error checking?
John
"Julian" <Julian@.discussions.microsoft.com> wrote in message
news:90531DDC-DAF8-4A74-9BEB-3FF5B01F7081@.microsoft.com...
> Hi,
> I have a SQL Agent task that fails, however the session carries on
> regradless and compeltes the task successfully. The task is a call to a
> stored procedure, and the Agent just gives an error of unknown token.
> Running SQL 2000 sp3a on Windows nt4 server.
> Can anyone shed any light on this?