Showing posts with label system. Show all posts
Showing posts with label system. Show all posts

Monday, March 26, 2012

Outer Join Problem

Because we have a lot of different kinds of data that we track, the system that I use uses an Entity-Attribute-Value data model. (Think of the entity id being the row, the attribute id being the column, and the value being a cell value.)

I want to be able to get all the values for an entity in one select. Unfortunately, most can be null, in which case we don't store a row for that value at all in the values table. So I'd like to get everything with an outer join. Unfortunately, that doesn't work very well.

This SQL behaves like you'd expect.

SELECT Event.Event_ID, String_Value.Value, String_Value.Attribute_ID

FROM Event LEFT OUTER JOIN

String_Value ON Event.Event_ID = String_Value.Event_ID

WHERE (Event.Event_ID = 35632)

(It returns 1 row.)

If I add a constraint to the String_Value table, all of a sudden, no rows get returned.

SELECT Event.Event_ID, String_Value.Value, String_Value.Attribute_ID

FROM Event LEFT OUTER JOIN

String_Value ON Event.Event_ID = String_Value.Event_ID

WHERE (Event.Event_ID = 35632) AND (String_Value.Attribute_ID = 69)

How can I revise this to always get the Event ID, but only get string values where the Attribute ID is 69? (This is extremely simplified. What I really want to do involves 7 left outer joins.)

I could do this using a temp table, doing updates for each value, but I'd like to keep things simple, if possible.

Try this

SELECT Event.Event_ID, String_Value.Value, String_Value.Attribute_ID

FROM Event LEFT OUTER JOIN

String_Value ON Event.Event_ID = String_Value.Event_ID
AND (String_Value.Attribute_ID = 69)

WHERE (Event.Event_ID = 35632)

Denis the SQL Menace

http://sqlservercode.blogspot.com/

|||

Criteria on the WHERE clause have to be true for the data set, criteria on the JOIN condition only have to be true when rows exist on the join table.

Thats why you should put your criteria on the join clause

Friday, March 9, 2012

OSQL Question

I created a sql script that transfers data from my test system to production adn vice versa. I would like to use OSQL and be able to pass parameters to the SQL script.

Example: param 1 = test; param 2 = production.

Can anyone tell me if this is possible and how to do it?

Thanks in advance for all the help.You can't directly pass parameters as part of the OSQL command-line utility, but you have several alternatives for solving your problem.

As stated in Winnet mag article :
OSQL won't let you pass in parameters, but you can use the sp_executesql stored procedure, which can process parameterized SQL. (For detailed information about sp_executesql, see SQL Server Books OnlineBOL.) The following example lets you pass parameters into a T-SQL command but doesn't completely address how to pass a parameter as part of an OSQL command-line session (remember that you must issue the OSQL command from a command prompt):

osql -E -MyServer -Q "EXECUTE sp_executesql N'SELECT *
FROM northwind..
orders WHERE OrderId = @.OrderId' ,N'@.OrderId int' ,@.OrderId = 10248"
Another solution relies on the power of Windows to handle the parameterization for you. For DBAs who aren't familiar with Windows-level command-file processing, a batch file is a text file that has a .bat extension. Windows treats batch files as executables that run in the Command Prompt environment. You can think of batch files as mini programs that Windows runs.

You can simply create a file called SQLVariableBatch.bat, and put the following text in it:

osql -E -MyServer -Q "SELECT *
FROM northwind..orders WHERE OrderId = %1"
From the directory where you saved the .bat file, issue the following command from a command prompt window:

SQLVariableBatch 10248
When running this command, Windows will replace the %1 in the SQLVariableBatch.bat file with what comes after the batch file's name in the command linein this case, 10248. This is a simple example of batch processing in Windows; to learn more about batch files, see the Windows Help files.

You can also use Windows Scripting Host (WSH) to manage the parameterization. Using WSH for scripting and batch processing is much more flexible and powerful than using simple Windows batch files. I'm not a WSH expert, so I don't include an example of this solution, but I wanted to note that the option exists. For information about using WSH, see the MSDN Web site for Windows Script at

http://msdn.microsoft.com/library/default.asp?url=/nhp/default.asp?contentid=28001169 .|||Thanks for your help. I will try your suggestions and see if I can figure it out.

Wednesday, March 7, 2012

OSQL Performance Problem

System info:
Win 2003 Server, SQL Server 2000, test server - no outside access from other users (no other applications running)

Table info:
DocId(int), a(nvarchar(10)), b(nvarchar(20)), c(nvarchar(10)), d(nvarchar(20)), e(nvarchar(10))
DocId has a number, all other columns are null. No index

I am using a stored procedure that updates the values based on the DocId. I have an program that creates a sql script file that should be executed. Approx. 440000 lines.

Example:
Using TableName
Go
SET NOCOUNT ON
GO
exec sp_SPNAME @.docId=1, @.a = 'blah', @.b = 'blah', @.c = 'blah', @.d = 'blah', @.e = 'blah'
GO
exec sp_SPNAME @.docId=2, @.a = 'blah', @.b = 'blah', @.c = 'blah', @.d = 'blah', @.e = 'blah'
GO
repeats 440K.

Question: When I execute this script per osql.exe, the update takes more the 24 hours... Any suggestions?

Thanks in advance.System info:
Win 2003 Server, SQL Server 2000, test server - no outside access from other users (no other applications running)

Table info:
DocId(int), a(nvarchar(10)), b(nvarchar(20)), c(nvarchar(10)), d(nvarchar(20)), e(nvarchar(10))
DocId has a number, all other columns are null. No index

INDEX !!!

Put a clustered index on DocID.

CREATE CLUSTERED INDEX IXc_TableName_DocID ON TableName (DocID)
GO

Regards,

hmscott|||another route is to bulkcopy/insert all the new data into a table then do a single update against the base table. Index on docid would be desired when you start dml.|||Hi hmscott,

thanks for the reply and the sql. I added the index and, while it is much faster, it still takes more than 8 hours. Maybe this is normal for executing 440,000 statements?

Thanks,
Lens|||Hi oj,

thanks to you as well for the reply. I will change my program to make a csv file and see if a bulk update increases the speed.

Thanks,
Lens|||Hi,

just a quick status update. I changed my program to create a csv-file. Approx. 440,000 lines imported into temp table; less than 2 minutes. Update into final table, less than two minutes...zoinks.

Thanks again.

Saturday, February 25, 2012

osql

I tried to issue this utitlity with T-SQL in my machine, but it asked me the
system administrator's password, how do I know this password at my local SQL
Server ?Alan wrote:
> I tried to issue this utitlity with T-SQL in my machine, but it asked
> me the system administrator's password, how do I know this password
> at my local SQL Server ?
Specify your loing credentials when you call osql. See BOL for more
information.
--
David Gugick
Imceda Software
www.imceda.com|||Alan
You may be able to use the -E switch to connect using a trusted connection.
ie. OSQL -Sservername -E
For a full list of OSQL switches refer to the osql Utility in SQL Server
Books on Line or run OSQL /? from the command line.
--
--
Peter Ward
WARDY IT Solutions
--
"Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
news:Od7KzB6yEHA.2016@.TK2MSFTNGP15.phx.gbl...
> I tried to issue this utitlity with T-SQL in my machine, but it asked me
the
> system administrator's password, how do I know this password at my local
SQL
> Server ?
>|||YOu may also use a standard SQL login like
osql -SServername -UyourSQLlogin -PPassword
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"P. Ward" <peter@.online.wardyit.com> wrote in message
news:%23aB1XQ9yEHA.2192@.TK2MSFTNGP14.phx.gbl...
> Alan
> You may be able to use the -E switch to connect using a trusted
connection.
> ie. OSQL -Sservername -E
> For a full list of OSQL switches refer to the osql Utility in SQL Server
> Books on Line or run OSQL /? from the command line.
> --
> --
> Peter Ward
> WARDY IT Solutions
> --
> "Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
> news:Od7KzB6yEHA.2016@.TK2MSFTNGP15.phx.gbl...
> > I tried to issue this utitlity with T-SQL in my machine, but it asked me
> the
> > system administrator's password, how do I know this password at my local
> SQL
> > Server ?
> >
> >
>