Showing posts with label statements. Show all posts
Showing posts with label statements. Show all posts

Friday, March 30, 2012

is it possible to recieve result of SQL stored procedure to web page?

I have web server with .aspx page from wich I call stored procedure on MSSQL server. In this procedure are lots of "select" statements and I need to show results of this statements in web page. Can I call this procedure in that manner that procedure output is writen in some file and this file then ir recieved by web server and included in web page.

for (int i=0; i<dataset.Tables.Count; i++) {
Response.Write(dataset.Tables[i].TableName);
Response.Write("\n");
// loop rows
for (int j=0; j<dataset.Tables[i].Rows.Count; j++) {
// loop columns
for (int k=0; k<dataset.Tables[i].Columns.Count; k++) {
Response.Write(dataset.Tables[i].Rows[j][k].ToString());
if (k < dataset.Tables[i].Columns.Count - 1) {
Response.Write(", ");
}
}
Response.Write("\n");
}
Response.Write("\n");
}

this isn't exactly what you want, but idea.|||If the procedure returns a fixed number of result sets, you can return the results a datareader and then bind each result set to something like a datagrid or a repeater using .nextresult between bindings. A datareader can hold more than one result set. It can be pretty handy.

Monday, March 26, 2012

Is it possible to generate alter Table statements using SMO

Hi

I'm trying to modify existing tables in a database.

How can I create alter Table scripts using SMO/DMO

Thank you

Yep, you can use the following to either execute and capture, just execute (which is the default) or just capture the executed commands:

Server s = new Server(".");

s.ConnectionContext.SqlExecutionModes = Microsoft.SqlServer.Management.Common.SqlExecutionModes.CaptureSql

//Microsoft.SqlServer.Management.Common.SqlExecutionModes.CaptureSql

//Microsoft.SqlServer.Management.Common.SqlExecutionModes.ExecuteAndCaptureSql

//Microsoft.SqlServer.Management.Common.SqlExecutionModes.ExecuteSql

//s.ConnectionContext.CapturedSql.Text; //Get the Text

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

Thanks again Jens

I'm trying the follwong code

Server server1 = new Server(".");

Database db= server1.Databases["master"];

server1.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql;

foreach (Table Tbl in db.Tables)

{

tabl.Alter ();

}

db1.Refresh();

//writing to a file

writeToFile(server1.ConnectionContext.CapturedSql.Text, "alter", "tables");

But it is not generating Alter statments.

But if I use Create(), in place of alter(), it's generating Create statments.

|||Hi,
if you do not change anything, what are you supposed to see in the ALTER script :-) ?

In this sample I added a column to the table resulting in a script with an ALTER Script and an ADD column command.

Server s = new Server(".");

s.ConnectionContext.SqlExecutionModes = Microsoft.SqlServer.Management.Common.SqlExecutionModes.CaptureSql;

Table t = s.Databases["SMOTest"].Tables["TestTable"];

t.Columns.Add(new Column(t,"SomeSMOTest",DataType.DateTime));

t.Alter();

foreach (string st in s.ConnectionContext.CapturedSql.Text)

{

Console.WriteLine(st);

}

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

Thanks Jens

Got it.

while comparing a table in one database to other table (identical) in other database,

if the Source table has some modified(altered) columns and need to be modified in the target table.

How to solve this problem.Any Idea.

I generated the alter scripts manually for each column.

Like

ALTER TABLE [dbo].[wo]

ADD [requested-time] varchar (8 ) NULL

Thank you

|||You will have to do this manually. Load the two schemas and compare the columns (if you just want to check the columns) with each other. Change the columns appropiately with SMO and get the script from the Context. if you want an integrated tool which can do this on its own use Visual Studio for database professionals, this does have a comparer and script generator for keeping the databases in sync.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de|||

Thanks alot Jens.

I'll try for this

Monday, March 19, 2012

Is it possible to audit Failed Insert, Update and Delete statements?

Auditors want us to track when Insert, Update and Delete failures occur. Is this possible in SQL 2000?
They also want us to track schema changes. Is this possible?
Thanks, DaveWhat constitutes a "failure" for the purpose of the audit?

-PatP|||The statement does not execute and returns an error. I believe I can trap this failures in Profiler, but I'm not sure what type of overhead this would create. Several people have suggested triggers, but I'm not sure a trigger will execute on failed attempts, only successfull insert, updates and deletes.

If I take the Profiler approach I'm not sure it will show schema changes.

Dave|||If you tell it to, SQL Profiler can track ANYTHING that goes to SQL Server. DML that works or fails, schema changes, and everything else. The question is: How much disk are you willing to dedicate to making this happen?

With the Profiler running "wide open" on a moderately busy server you are looking at 2-3 Tb of data in a 24 hour period... Once you've collected the data, you need to figure out what (if anything) you are going to do with it!

The old Chinese adage applies: Be careful what you wish for, you might get it!

-PatP|||We will only be monitoring two or three ids. Not sure if a domain group can be monitored, but if so we will monitor at the group level. This is for Sarbanes-Oxley complaince, which is basically very strict management of database systems for financial institutions. My thanks to Enron. Our DBAs are allowed to manage development and model office environments and a consulting company gets to manage production. Sarbanes-Oxley requires we keep an eye on the production DBAs by monitoring their activity. Profiler may not be the best approach. Even though we will be monitoring a small number of ids, SQL Server still needs to perform conditional logic against all user activity to see if the filter criteria is being met. A software tool may produce less overhead.

Thanks, Dave|||Everybody loves the joy of SOX!

If you really, really need to, you can get C2 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adminsql/ad_config_09yd.asp) auditing from SQL-2000. There are thousands of auditing combinations, many of which are pretty much designed for exactly what you want to do.

I'd be hard pressed to recommend a third party product for this use... At least in my opinion, it is likely to be more work than it is worth in the long run.

-PatP

Friday, March 9, 2012

Is it necessary to add a GO ?

We have to run two update statements:
update pthdbo.table14
set name = t3.sortname
from pthdbo.table14 as t1,
xxxxxx
AND
update pthdbo.table14
set streetname = t7.streetname
from pthdbo.table as t1,
yyyyyy
Can I run these 2 update statements together ? Is it
necessary for me to add a GO statement after running the
first UPDATE statement ? If YES, would you mind to let me
know why ?
ThanksJason
GO is a batch "breaker" and it is not TSQL command
If you run it on QA you may want to consider putting GO between an UPDATE
statements
"Jason" <anonymous@.discussions.microsoft.com> wrote in message
news:01e301c5b9bd$ab182150$a601280a@.phx.gbl...
> We have to run two update statements:
> update pthdbo.table14
> set name = t3.sortname
> from pthdbo.table14 as t1,
> xxxxxx
> AND
> update pthdbo.table14
> set streetname = t7.streetname
> from pthdbo.table as t1,
> yyyyyy
> Can I run these 2 update statements together ? Is it
> necessary for me to add a GO statement after running the
> first UPDATE statement ? If YES, would you mind to let me
> know why ?
> Thanks|||Additional information:
For the concept of batch, assume there are 10 statements in a batch. If the
fifth statement has a syntax error, none of the statements in the batch are
executed. If the batch is compiled, and the second statement then fails
while executing, the results of the first statement are not affected because
it has already executed.
Read the online help for more information and detailed explanation.
"Uri Dimant" <urid@.iscar.co.il> ¼¶¼g©ó¶l¥ó·s»D:OLbGPKcuFHA.3660@.tk2msftngp13.phx.gbl...
> Jason
> GO is a batch "breaker" and it is not TSQL command
> If you run it on QA you may want to consider putting GO between an UPDATE
> statements
>
> "Jason" <anonymous@.discussions.microsoft.com> wrote in message
> news:01e301c5b9bd$ab182150$a601280a@.phx.gbl...
>> We have to run two update statements:
>> update pthdbo.table14
>> set name = t3.sortname
>> from pthdbo.table14 as t1,
>> xxxxxx
>> AND
>> update pthdbo.table14
>> set streetname = t7.streetname
>> from pthdbo.table as t1,
>> yyyyyy
>> Can I run these 2 update statements together ? Is it
>> necessary for me to add a GO statement after running the
>> first UPDATE statement ? If YES, would you mind to let me
>> know why ?
>> Thanks
>|||Dear all,
Thank you for your advice.
In this way, I can add GO after both Update Statement. However, it seems
that the one after the 2nd Update Statement is optional AS there is nothing
behind it ?
use database1
GO
update pthdbo.table14
set name = t3.sortname
from pthdbo.table14 as t1,
xxxxxx
GO
update pthdbo.table14
set streetname = t7.streetname
from pthdbo.table as t1,
yyyyyy
GO
Thanks
"Lau Lei Cheong" <leu_lc@.yehoo.com.hk> wrote in message
news:edEK12cuFHA.3896@.TK2MSFTNGP15.phx.gbl...
> Additional information:
> For the concept of batch, assume there are 10 statements in a batch. If
> the fifth statement has a syntax error, none of the statements in the
> batch are executed. If the batch is compiled, and the second statement
> then fails while executing, the results of the first statement are not
> affected because it has already executed.
> Read the online help for more information and detailed explanation.
> "Uri Dimant" <urid@.iscar.co.il>
> ¼¶¼g©ó¶l¥ó·s»D:OLbGPKcuFHA.3660@.tk2msftngp13.phx.gbl...
>> Jason
>> GO is a batch "breaker" and it is not TSQL command
>> If you run it on QA you may want to consider putting GO between an
>> UPDATE statements
>>
>> "Jason" <anonymous@.discussions.microsoft.com> wrote in message
>> news:01e301c5b9bd$ab182150$a601280a@.phx.gbl...
>> We have to run two update statements:
>> update pthdbo.table14
>> set name = t3.sortname
>> from pthdbo.table14 as t1,
>> xxxxxx
>> AND
>> update pthdbo.table14
>> set streetname = t7.streetname
>> from pthdbo.table as t1,
>> yyyyyy
>> Can I run these 2 update statements together ? Is it
>> necessary for me to add a GO statement after running the
>> first UPDATE statement ? If YES, would you mind to let me
>> know why ?
>> Thanks
>>
>|||No. The first GO signals begining of a batch process(i.e. put it in batch
processing mode), and the second GO signal that's the end of it so it gets
executed.
"Jason" <anonymous@.discussions.microsoft.com> ¼¶¼g©ó¶l¥ó·s»D:OVvXrNeuFHA.3596@.TK2MSFTNGP15.phx.gbl...
> Dear all,
> Thank you for your advice.
> In this way, I can add GO after both Update Statement. However, it seems
> that the one after the 2nd Update Statement is optional AS there is
> nothing behind it ?
> use database1
> GO
> update pthdbo.table14
> set name = t3.sortname
> from pthdbo.table14 as t1,
> xxxxxx
> GO
> update pthdbo.table14
> set streetname = t7.streetname
> from pthdbo.table as t1,
> yyyyyy
> GO
> Thanks
> "Lau Lei Cheong" <leu_lc@.yehoo.com.hk> wrote in message
> news:edEK12cuFHA.3896@.TK2MSFTNGP15.phx.gbl...
>> Additional information:
>> For the concept of batch, assume there are 10 statements in a batch. If
>> the fifth statement has a syntax error, none of the statements in the
>> batch are executed. If the batch is compiled, and the second statement
>> then fails while executing, the results of the first statement are not
>> affected because it has already executed.
>> Read the online help for more information and detailed explanation.
>> "Uri Dimant" <urid@.iscar.co.il> ¼¶¼g©ó¶l¥ó·s»D:OLbGPKcuFHA.3660@.tk2msftngp13.phx.gbl...
>> Jason
>> GO is a batch "breaker" and it is not TSQL command
>> If you run it on QA you may want to consider putting GO between an
>> UPDATE statements
>>
>> "Jason" <anonymous@.discussions.microsoft.com> wrote in message
>> news:01e301c5b9bd$ab182150$a601280a@.phx.gbl...
>> We have to run two update statements:
>> update pthdbo.table14
>> set name = t3.sortname
>> from pthdbo.table14 as t1,
>> xxxxxx
>> AND
>> update pthdbo.table14
>> set streetname = t7.streetname
>> from pthdbo.table as t1,
>> yyyyyy
>> Can I run these 2 update statements together ? Is it
>> necessary for me to add a GO statement after running the
>> first UPDATE statement ? If YES, would you mind to let me
>> know why ?
>> Thanks
>>
>>
>

Wednesday, March 7, 2012

Is it a bug of SQL Server 2000 SP4?

Database backup file:
http://www.keepmyfile.com/download/c58b2a565144
Environment:
SQL Server 2000 SP4
Problem:
The following two statements returns different number of records:
Exec GenPeriodical1 102, null, '20050601', '20050630', null, null, 0
SELECT *
FROM dbo.OtherFee (null, '20050601', '20050630', null, null, 0)
WHERE flow_id = 102
This problem wasn't found in SQL Server 2000 original version and SQL Server
2005.
Any help is appreciated!Well, seems like a bug.
You can fix it by rearranginf the FROM clause in the function in the
following manner.
FROM action_room_req2 arr
JOIN flow_action fa ON arr.flow_id = fa.flow_id
JOIN cust_action ca ON ca.valid = 0 AND fa.action_id = ca.id
JOIN action_room ar ON ar.action_id = ca.id AND ar.code = 0
JOIN customer c ON ca.customer_id = c.id
LEFT JOIN turn_rule tr ON arr.req_type = 'Mall' and arr.ref_id = tr.id
Regards
Roji. P. Thomas
http://toponewithties.blogspot.com
"hghua" <hghua@.discussions.microsoft.com> wrote in message
news:A9FB112B-AD45-463A-A167-CFAF77E20B5D@.microsoft.com...
> Database backup file:
> http://www.keepmyfile.com/download/c58b2a565144
> Environment:
> SQL Server 2000 SP4
> Problem:
> The following two statements returns different number of records:
> Exec GenPeriodical1 102, null, '20050601', '20050630', null, null, 0
> SELECT *
> FROM dbo.OtherFee (null, '20050601', '20050630', null, null, 0)
> WHERE flow_id = 102
> This problem wasn't found in SQL Server 2000 original version and SQL
> Server
> 2005.
> Any help is appreciated!|||Thanks a lot! That works!
Hope Microsoft will solve the problem.
"Roji. P. Thomas" wrote:

> Well, seems like a bug.
> You can fix it by rearranginf the FROM clause in the function in the
> following manner.
> FROM action_room_req2 arr
> JOIN flow_action fa ON arr.flow_id = fa.flow_id
> JOIN cust_action ca ON ca.valid = 0 AND fa.action_id = ca.id
> JOIN action_room ar ON ar.action_id = ca.id AND ar.code = 0
> JOIN customer c ON ca.customer_id = c.id
> LEFT JOIN turn_rule tr ON arr.req_type = 'Mall' and arr.ref_id = tr.id
> --
> Regards
> Roji. P. Thomas
> http://toponewithties.blogspot.com
> "hghua" <hghua@.discussions.microsoft.com> wrote in message
> news:A9FB112B-AD45-463A-A167-CFAF77E20B5D@.microsoft.com...
>
>

Is it a bug of SQL Server 2000 SP4?

Database backup file: 
http://www.keepmyfile.com/download/c58b2a565144
Environment:
SQL Server 2000 SP4
Problem:
The following two statements returns different number of records:
Exec GenPeriodical1 102, null, '20050601', '20050630', null, null, 0
SELECT *
FROM dbo.OtherFee (null, '20050601', '20050630', null, null, 0)
WHERE flow_id = 102
This problem wasn't found in SQL Server 2000 original version and SQL Server 
2005.
Any help is appreciated!
Is it possible to see text for dbo.OtherFee and GenPeriodical1?|||

Thanks for your reply!

I've got the answer from the newsgroup. The replyer said it seems a bug of SP4 and gave a work-around. If you are interested in this issue, you can download the backup file, it's just 1.18MB.

The store proc and function call other functions, thus not convenient to paste them here.

Friday, February 24, 2012

is Exists faster than a Join

From http://www.sql-server-performance.com/query_analysis_tuning.asp
B) Can standard JOIN's be replaced with EXISTS statements?
If you need to join tables to ensure XYZ value exists or a specific set of
rows exists, then considering porting to EXISTS or NOT EXISTS statements.
At the same time is may also alleviate the need for DISTINCT clauses.
So is a Join really potentiall slower?The Exists clause can be faster than a join because it just searches long
enough to find the first "Hit" as opposed to returning all rows that meet
the join criteria.
Greg Jackson
PDX, Oregon|||Could you please elaborate? An Exists will meet all the rows that meet the
query criteria. Given that the criteria should return the same data as a
join, how could it be faster?
"pdxJaxon" <GregoryAJackson@.Hotmail.com> wrote in message
news:eZWWYB7aFHA.3848@.TK2MSFTNGP10.phx.gbl...
> The Exists clause can be faster than a join because it just searches long
> enough to find the first "Hit" as opposed to returning all rows that meet
> the join criteria.
>
> Greg Jackson
> PDX, Oregon
>|||I say
SELECT * FROM Main
WHERE Exists(SELECT 1 FROM MyJoinTable a WHERE a.foo = main.foo)
then it will only check for the first hit.
still probably not making sense.
show us YOUR query and I can probably explain a bit better
GAJ|||create table #Table1(T1C1 int identity (1,1), T1C2 char(10))
create table #Table2(T2C1 int, T2C2 char(10))
insert into #Table1 (T1C2) values ('T1C2')
insert into #Table1 (T1C2) values ('T1C2-2')
insert into #Table2 (T2C1,T2C2) values (1,'T1C2')
select t1.T1C1,T1C2
from #Table1 t1
inner join #Table2 t2 on t1.t1c1 = t2.t2c1
or
select t1.T1C1,T1C2
from #table1 t1
where exists
(select * from #Table2 t2
where t2.T2C1 = T1.T1C1)
"pdxJaxon" <GregoryAJackson@.Hotmail.com> wrote in message
news:%23fZBCd7aFHA.464@.TK2MSFTNGP15.phx.gbl...
>I say
> SELECT * FROM Main
> WHERE Exists(SELECT 1 FROM MyJoinTable a WHERE a.foo = main.foo)
> then it will only check for the first hit.
> still probably not making sense.
> show us YOUR query and I can probably explain a bit better
>
> GAJ
>|||that's faster than a Join.
the where clause will return TRUE as soon as it finds a SINGLE record in
TABLE 2 that mathces Table1 (It will not bother returning more than 1 record
that matches to table 1)
A join will return ALL Records in T2 that match T1. IF there are millions of
records in each table, this would be exponentially faster.
Greg Jackson
PDX, Oregon|||On Tue, 7 Jun 2005 15:11:21 -0700, ChrisR wrote:
>From http://www.sql-server-performance.com/query_analysis_tuning.asp
>B) Can standard JOIN's be replaced with EXISTS statements?
>If you need to join tables to ensure XYZ value exists or a specific set of
>rows exists, then considering porting to EXISTS or NOT EXISTS statements.
>At the same time is may also alleviate the need for DISTINCT clauses.
>
>So is a Join really potentiall slower?
Hi Chris,
The question is actually irrelevant. A join is different: a query such
as
SELECT a.*
FROM a
WHERE EXISTS (SELECT * FROM b WHERE b.xx = a.xx)
will never return more than one copy of each row in a. Using a join
instead, such as this:
SELECT a.*
FROM a
JOIN b ON b.xx = a.xx
might return multiple copies of rows in a for values of xx that are not
unique in table b.
If no value of xx is ever duplicated in b, then the results will be the
same, but the EXISTS version might still be faster. Since SQL Server
doesn't know that there is only one row in b with a particular value of
xx, it still has to search the rest of the table after encoutering one;
if EXISTS is used, the search is aborted after the first match.
If SQL Server knows that values of b.xx are unique (because there is a
PRIMARY KEY or UNIQUE constraint on b.xx), then the performance MIGHT be
the same - the optimizer MIGHT decide to use the knowledge of this
constraint and createt the same execution plan it'll also use for the
EXISTS version.
I have never seen or heard of any situation where the JOIN would be
fafster than the "equivalent" EXISTS version. And I don't think I ever
will.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Hi Hugo and Chris,
"Hugo Kornelis" wrote:
> On Tue, 7 Jun 2005 15:11:21 -0700, ChrisR wrote:
> >From http://www.sql-server-performance.com/query_analysis_tuning.asp
> >
> >B) Can standard JOIN's be replaced with EXISTS statements?
> >If you need to join tables to ensure XYZ value exists or a specific set of
> >rows exists, then considering porting to EXISTS or NOT EXISTS statements.
> >At the same time is may also alleviate the need for DISTINCT clauses.
> >
> >So is a Join really potentiall slower?
> Hi Chris,
> The question is actually irrelevant. A join is different: a query such
> as
> SELECT a.*
> FROM a
> WHERE EXISTS (SELECT * FROM b WHERE b.xx = a.xx)
> will never return more than one copy of each row in a. Using a join
> instead, such as this:
> SELECT a.*
> FROM a
> JOIN b ON b.xx = a.xx
> might return multiple copies of rows in a for values of xx that are not
> unique in table b.
> If no value of xx is ever duplicated in b, then the results will be the
> same, but the EXISTS version might still be faster. Since SQL Server
> doesn't know that there is only one row in b with a particular value of
> xx, it still has to search the rest of the table after encoutering one;
> if EXISTS is used, the search is aborted after the first match.
> If SQL Server knows that values of b.xx are unique (because there is a
> PRIMARY KEY or UNIQUE constraint on b.xx), then the performance MIGHT be
> the same - the optimizer MIGHT decide to use the knowledge of this
> constraint and createt the same execution plan it'll also use for the
> EXISTS version.
> I have never seen or heard of any situation where the JOIN would be
> fafster than the "equivalent" EXISTS version. And I don't think I ever
> will.
>
I think you have to try the statement both ways.
I'm not sure about EXISTS, but I have a fairly complex multi-way join that
is much faster with
a LEFT JOIN b on a.id = b.id ... WHERE b.id is null
than with
WHERE NOT EXISTS (select 1 from b where a.id = b.id)
Regards,
Jonathan|||On Thu, 9 Jun 2005 12:52:03 -0700, Jonathan Levine wrote:
(snip)
>I'm not sure about EXISTS, but I have a fairly complex multi-way join that
>is much faster with
>a LEFT JOIN b on a.id = b.id ... WHERE b.id is null
>than with
>WHERE NOT EXISTS (select 1 from b where a.id = b.id)
Hi Jonathan,
Thanks for adding this. This is not the first time I witness or hear
reports of this either. It seems that the SQL Server development team
put most effort in the optimization of join strategies, since they are
used most often in typical queries.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Also, be careful of the Optimizer Estimated Execution Plans. I especially
see this in comparisons between LEFT OUTER JOINS versus the use of EXISTS or
NOT EXISTS.
Even though the JOIN may produce a better ESTIMATED execution cost, it will
often create a HASH table for the JOIN, where the EXISTS solution will often
use a Nested Loop JOIN. The HASH will ALWAYS create temporary object, which
will have physical impact that the Optimizer does not take into account.
You're best bet would be to SET STATISTICS time and I/O on and run them both
ways, making sure to flush the Buffer Pool before each execution. This will
give you ACTUAL run information, not just ESTIMATED.
Sincerely,
Anthony Thomas
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:mnfha11l8fukurbcocmi9rg30s47q0lj6j@.4ax.com...
On Thu, 9 Jun 2005 12:52:03 -0700, Jonathan Levine wrote:
(snip)
>I'm not sure about EXISTS, but I have a fairly complex multi-way join that
>is much faster with
>a LEFT JOIN b on a.id = b.id ... WHERE b.id is null
>than with
>WHERE NOT EXISTS (select 1 from b where a.id = b.id)
Hi Jonathan,
Thanks for adding this. This is not the first time I witness or hear
reports of this either. It seems that the SQL Server development team
put most effort in the optimization of join strategies, since they are
used most often in typical queries.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)