Showing posts with label single. Show all posts
Showing posts with label single. Show all posts

Tuesday, March 20, 2012

ALTER TABLE from sqlcmd script

Hi,

I'm trying to add a column to a table, then update that column with a
query. This is all within a single batch. Sqlcmd gives me an error on
the update, saying "invalid column xxx", because it doesn't know the
column got added. We used to get around this in "osql" by using the
EXECUTE command, like: EXEC ("ALTER TABLE tbl ADD newfield varchar(255)
not null default ' '")

However, it looks like sqlcmd actually checks each query within the
script before it starts running, and throws the error because the field
isn't there at the time.

If need be I can just do a SELECT INTO and add the column there, but
it's a pain in the butt and I'm moving a LOT of data just to do what I
want. And no, I can't go back to where the table is created and add
the column. Does anyone have any suggestions? TIA!

- JeffHi,

put a GO after the ALTER TABLEa nd you should be done.

HTH, Jens Suessmeyer.

--
http://www.sqlserver2005.de
--|||Jeff_in_MD (jfowler@.dsoftware.biz) writes:

Quote:

Originally Posted by

I'm trying to add a column to a table, then update that column with a
query. This is all within a single batch. Sqlcmd gives me an error on
the update, saying "invalid column xxx", because it doesn't know the
column got added. We used to get around this in "osql" by using the
EXECUTE command, like: EXEC ("ALTER TABLE tbl ADD newfield varchar(255)
not null default ' '")
>
However, it looks like sqlcmd actually checks each query within the
script before it starts running, and throws the error because the field
isn't there at the time.


The full story is that SQL Server never accepts a missing column. Still
you sometimes you get away with it. Why? Because of deferred name
resolution (one of the biggest misfeatures added in SQL 7). Deferred
name resolution means that if SQL Server finds a query in a batch, where
one or more tables are missing, it defers compilation until later, and
you will not get an error, unless execution reaches that query and the
table is still missing. Quite an aggravated cost for plain spelling
errors!

But if all tables in a query exists, SQL Server also requires that all
columns exist. Thankfully, there is no deferred name resolution on
columns!

The actual effect of these rules is a bit different in SQL 2000 and
SQL 2005, since in SQL 2000, the entire batch is always recompiled,
while SQL 2005 has statment recompile.

Anyway, the proper procedure in a case like yours is to put all
statements that refer to the new column in EXEC, so that they are
compiled after the new column was added. There is not really any
need to put the ALTER statement in EXEC though.

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

Friday, February 24, 2012

Almost there (I think)...SQL Update problem...

I am trying to update a single field in a SQL database table. I created a SQLDataSource, configured Select and Update queries, and wrote some code in the script block to do the update after a button click. The SQLDataSource is in a contentplaceholder. What am I doing wrong in the data source, the script block, or both? Thanks so much in advance...

Here is the code for the SQLDataSource:

Dim ImageUploaded As Integer = 2


srcUpdateImageUploaded.UpdateParameters("@.ImageUploaded").DefaultValue = ImageUploaded

srcUpdateImageUploaded.Update()

Here is the code in the script block:

<asp:SqlDataSource ID="srcUpdateImageUploaded" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserProfilesDB.mdf;Integrated Security=True;User Instance=True"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [ImageUploaded] FROM [profiles_BasicProperties] WHERE ([UserName] = @.UserName)"
UpdateCommand="UPDATE profiles_BasicProperties SET [ImageUploaded] = ?">
<UpdateParameters>
<asp:ControlParameter ControlID="TextBox1" Name="EmilyTheKitty" PropertyName="Text" Type="Object" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="UserName" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

Here is the error that I get:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 164: Dim ImageUploaded As Integer = 2
Line 165:
Line 166: srcUpdateImageUploaded.UpdateParameters("@.ImageUploaded").DefaultValue = ImageUploaded
Line 167: srcUpdateImageUploaded.Update()
Line 168:

Source File: C:\Users\Matthew\Documents\Group 02 - Politicore\PC_Dev\Profiles_BuildProfile.aspx Line: 166

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
ASP.profiles_buildprofile_aspx.PictureUpload(Object sender, EventArgs e) in C:\Users\Matthew\Documents\Group 02 - Politicore\PC_Dev\Profiles_BuildProfile.aspx:166
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +104
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5614


There is no @.ImageUploaded parameter in your SQLDataSource, See the modified code below

<asp:SqlDataSource ID="srcUpdateImageUploaded" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserProfilesDB.mdf;Integrated Security=True;User Instance=True"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [ImageUploaded] FROM [profiles_BasicProperties] WHERE ([UserName] = @.UserName)"
UpdateCommand="UPDATE profiles_BasicProperties SET [ImageUploaded] = @.ImageUploaded">
<UpdateParameters>
<asp:ControlParameter ControlID="TextBox1" Name="ImageUploaded" PropertyName="Text" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="UserName" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

|||

I appreciate the response...but it didn't work. I tried the modified code you posted. I pasted it into my page, tried it, and got the "Object reference not set to an instance of an object" again. Here is the code that I copied out of my page (it's the same code you posted)...

<asp:SqlDataSourceID="srcUpdateImageUploaded"runat="server"ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserProfilesDB.mdf;Integrated Security=True;User Instance=True"

ProviderName="System.Data.SqlClient"

SelectCommand="SELECT [ImageUploaded] FROM [profiles_BasicProperties] WHERE ([UserName] = @.UserName)"

UpdateCommand="UPDATE profiles_BasicProperties SET [ImageUploaded] = @.ImageUploaded">

<UpdateParameters>

<asp:ControlParameterControlID="TextBox1"Name="ImageUploaded"PropertyName="Text"Type="Int32"/>

</UpdateParameters>

<SelectParameters>

<asp:ControlParameterControlID="TextBox1"Name="UserName"PropertyName="Text"Type="String"/>

</SelectParameters>

</asp:SqlDataSource>

|||

where you are trying to update your code?? is it in the page load event ?? and also try to change asp:controlParameter into asp:FormParameter. If still not solved pls paste the whole code I will look into it

|||

Okay...I changed my mind on how I want to do this. I did away with the SQL data source connection object and I want to do this entirely with code in the script block. There is a command button that is clicked which invokes the following code. A textbox is included on the page and it is called by the code. The new error I get is this:

" Error updating table. Must declare the scalar variable "@.updatevalue". "

Here is the entirety of the code that is called:

ProtectedSub cmdUpdate_Click(ByVal senderAsObject, _

ByVal eAs EventArgs)Handles cmdUpdate.Click

'a temporary variable that is hard coded to 2 for testing...

Dim updatevalueAs Int32

updatevalue = 2

Dim usernameAsString

username = txtUserName.Text

Dim connectionstringAsString

connectionstring ="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserProfilesDB.mdf;Integrated Security=True;User Instance=True"

' Define ADO.NET objects.

Dim updateSQLAsString

updateSQL ="UPDATE profiles_BasicProperties SET "

updateSQL &="ImageUploaded=@.updatevalue "

updateSQL &="WHERE username=@.username"

Dim conAsNew SqlConnection(connectionString)

Dim cmdAsNew SqlCommand(updateSQL, con)

' Add the parameters.

cmd.Parameters.AddWithValue("@.ImageUploaded", updatevalue)

' Try to open database and execute the update.

Try

con.Open()

Dim updatedAsInteger = cmd.ExecuteNonQuery()

lblResults.Text = updated.ToString() &" records updated."

Catch errAs Exception

lblresults.Text ="Error updating table. "

lblResults.Text &= err.Message

Finally

con.Close()

EndTry

EndSub

|||

Okay, I fixed my own problem. I also figured out how these lines are put together so I am beyond merely cutting and pasting code in from books. Here is the correct code (corrected lines in bold, italics, and underlined):

ProtectedSub cmdUpdate_Click(ByVal senderAsObject, _

ByVal eAs EventArgs)Handles cmdUpdate.Click

'a temporary variable that is hard coded to 2 for testing...

Dim updatevalueAs Int32

updatevalue = 2

Dim usernameAsString

username = txtUserName.Text

Dim connectionstringAsString

connectionstring ="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserProfilesDB.mdf;Integrated Security=True;User Instance=True"

' Define ADO.NET objects.

Dim updateSQLAsString

updateSQL ="UPDATE profiles_BasicProperties SET "

updateSQL &="ImageUploaded=@.ImageUploaded "

updateSQL &="WHERE username=@.username"

Dim conAsNew SqlConnection(connectionString)

Dim cmdAsNew SqlCommand(updateSQL, con)

' Add the parameters.

cmd.Parameters.AddWithValue("@.ImageUploaded", updatevalue)

cmd.Parameters.AddWithValue("@.username", username)

' Try to open database and execute the update.

Try

con.Open()

Dim updatedAsInteger = cmd.ExecuteNonQuery()

lblResults.Text = updated.ToString() &" records updated."

Catch errAs Exception

lblresults.Text ="Error updating table. "

lblResults.Text &= err.Message

Finally

con.Close()

EndTry

EndSub

Thursday, February 16, 2012

'Allow null values' causes report to run automatically

Hi
I have a long running report. The report is based on a SQL Server 2005
stored procedure, with a single Integer parameter. I want to allow the
user to choose NULL for the parameter, but I don't want the report to
run without the user clicking 'View Report'. But I am finding (from
Report Parameters) when I choose 'Allow null values' there is no
default value of 'None'. I don't want to specify a default value,
because this causes the report to execute immediately.
Has anybody come across this?
cheers,
TJWhat you need, and what would be nice, is a default option of None
(not Null) which doesn't allow the report to run until the user
selects a value. Unfortunatley, the only default options you have
are: Non-queried, From query and Null.
The simplest solution is probably to set a Non-queried default value
that is invalid in your report. This will till cause your report to
run but will return an empty result set. If it's a very simple report
you could then set the visibility of your elements to not show, or
possibly show "Please select a value", if this default invalid value
is given. Otherwise you'll get an empty looking report or, worse yet,
an error; either result would probably be confusing to the user.
Matt Penner
On Jan 2, 7:28 pm, TJ <newsgroups...@.gmail.com> wrote:
> Hi
> I have a long running report. The report is based on a SQL Server 2005
> stored procedure, with a single Integer parameter. I want to allow the
> user to choose NULL for the parameter, but I don't want the report to
> run without the user clicking 'View Report'. But I am finding (from
> Report Parameters) when I choose 'Allow null values' there is no
> default value of 'None'. I don't want to specify a default value,
> because this causes the report to execute immediately.
> Has anybody come across this?
> cheers,
> TJ

Allow both Anonymous Access and Windows Integrated Access on RS

How can I configure IIS to allow anonymous and integrated requests to a
single Reporting Services 2005 installation?
If I try to mix the two securities (anonymous access and Windows integrated
authentication), all requests run as anonymous (under the IUSR account) and
therefore I get an access denied errors when trying to access a report/folder
that does not allow IUSR as a report "browser").
I have tried to set up a separate web site on the IIS box that points to the
Reporting Sever folders that implements either security; but with out
success. It appears that RS 2005 requires to be installed on the "Default
Web Site"?
--
LulkTwo different questions here. The two websites that need to be running as
integrated are: reports and reportserver. Your other websites can run
anonoymous.
Your second question: As far as the default website. The install for 2000
made it very difficult to not have it as the default. However, with 2005
this should be possible. I haven't done it. For this question (how to
install where not the default website), I suggest another posting so someone
will know your question and can answer it.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Lulk" <Lulk@.discussions.microsoft.com> wrote in message
news:05F07710-5C48-47F2-B1BC-EA8E8D768A29@.microsoft.com...
> How can I configure IIS to allow anonymous and integrated requests to a
> single Reporting Services 2005 installation?
> If I try to mix the two securities (anonymous access and Windows
> integrated
> authentication), all requests run as anonymous (under the IUSR account)
> and
> therefore I get an access denied errors when trying to access a
> report/folder
> that does not allow IUSR as a report "browser").
> I have tried to set up a separate web site on the IIS box that points to
> the
> Reporting Sever folders that implements either security; but with out
> success. It appears that RS 2005 requires to be installed on the "Default
> Web Site"?
> --
> Lulk|||I have a single web site set up to strictly to access Reporting Services.
One I want to run Integrated and allow Anonymous. I tried creating a separate
web site that points to the same app directories, but that's not working
cleanly.
--
Lulk
"Bruce L-C [MVP]" wrote:
> Two different questions here. The two websites that need to be running as
> integrated are: reports and reportserver. Your other websites can run
> anonoymous.
> Your second question: As far as the default website. The install for 2000
> made it very difficult to not have it as the default. However, with 2005
> this should be possible. I haven't done it. For this question (how to
> install where not the default website), I suggest another posting so someone
> will know your question and can answer it.
>
> --
> Bruce Loehle-Conger
> MVP SQL Server Reporting Services
> "Lulk" <Lulk@.discussions.microsoft.com> wrote in message
> news:05F07710-5C48-47F2-B1BC-EA8E8D768A29@.microsoft.com...
> > How can I configure IIS to allow anonymous and integrated requests to a
> > single Reporting Services 2005 installation?
> > If I try to mix the two securities (anonymous access and Windows
> > integrated
> > authentication), all requests run as anonymous (under the IUSR account)
> > and
> > therefore I get an access denied errors when trying to access a
> > report/folder
> > that does not allow IUSR as a report "browser").
> > I have tried to set up a separate web site on the IIS box that points to
> > the
> > Reporting Sever folders that implements either security; but with out
> > success. It appears that RS 2005 requires to be installed on the "Default
> > Web Site"?
> > --
> > Lulk
>
>|||You cannot run both anonymous and integrated. That just doesn't work. Once
you are anonymous you are anoymous.
If you are in an intranet environment, most domains have a generic Users
group which is everybody in that domain. That would allow you to let
everyone access to the report.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Lulk" <Lulk@.discussions.microsoft.com> wrote in message
news:E2CC088F-2D45-4BD1-91C7-409823061287@.microsoft.com...
>I have a single web site set up to strictly to access Reporting Services.
> One I want to run Integrated and allow Anonymous. I tried creating a
> separate
> web site that points to the same app directories, but that's not working
> cleanly.
> --
> Lulk
>
> "Bruce L-C [MVP]" wrote:
>> Two different questions here. The two websites that need to be running as
>> integrated are: reports and reportserver. Your other websites can run
>> anonoymous.
>> Your second question: As far as the default website. The install for 2000
>> made it very difficult to not have it as the default. However, with 2005
>> this should be possible. I haven't done it. For this question (how to
>> install where not the default website), I suggest another posting so
>> someone
>> will know your question and can answer it.
>>
>> --
>> Bruce Loehle-Conger
>> MVP SQL Server Reporting Services
>> "Lulk" <Lulk@.discussions.microsoft.com> wrote in message
>> news:05F07710-5C48-47F2-B1BC-EA8E8D768A29@.microsoft.com...
>> > How can I configure IIS to allow anonymous and integrated requests to a
>> > single Reporting Services 2005 installation?
>> > If I try to mix the two securities (anonymous access and Windows
>> > integrated
>> > authentication), all requests run as anonymous (under the IUSR account)
>> > and
>> > therefore I get an access denied errors when trying to access a
>> > report/folder
>> > that does not allow IUSR as a report "browser").
>> > I have tried to set up a separate web site on the IIS box that points
>> > to
>> > the
>> > Reporting Sever folders that implements either security; but with out
>> > success. It appears that RS 2005 requires to be installed on the
>> > "Default
>> > Web Site"?
>> > --
>> > Lulk
>>

Monday, February 13, 2012

Allocate to records, randomize rounded amount

I'd like to build a stored procedure that would allocate a single value
among records. If there was a rounded remainder I would like to randomly
pick one of the records to receive the "extra" rounded amount. I'd like to
pass my procedure 2 variables: @.AccountType and @.Amount. The amount
allocated should always be to 2 decimal places.
If the variables were @.AccountType = "A' and @.Amount = 10 the AccountValue
for both AccountIDs 1 and 2 would be 5.00
If the variables were @.AccountType = "A' and @.Amount = 11 the AccountValue
for both AccountIDs 1 and 2 would be 5.50
If the variables were @.AccountType = "B' and @.Amount = 10 the AccountValue
would be 3.33 for 2 of the accounts and 3.34 for the third account. I'd like
to randomize which account gets 3.34, the extra penny.
Can anyone help me write this stored procedure?
CREATE TABLE [dbo].[Accounts] (
[AccountID] [int] NOT NULL ,
[AccountType] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[AccountValue] [decimal](18, 2) NULL
) ON [PRIMARY]
GO
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(1,'A',NULL)
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(2,'A',NULL)
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(3,'B',NULL)
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(4,'B',NULL)
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(5,'B',NULL)
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(6,'C',NULL)
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(7,'C',NULL)
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(8,'C',NULL)
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(9,'C',NULL)
INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
(10,'C',NULL)e.g.
alter procedure AllocateToAccounts(@.AccountType char(1), @.TotalValue
decimal(18,2))
as
declare
@.Allocation decimal(18,2),
@.Count int, @.RandID int
select @.Count = count(*)
from Accounts
where AccountType = @.AccountType
select @.RandID = (
select top 1 AccountID
from Accounts
where AccountType=@.AccountType
order by NewID()
)
select @.Allocation = @.TotalValue / @.Count
update Accounts
set AccountValue =
case AccountID
when @.RandID then @.Allocation + (@.TotalValue-(@.Allocation*@.Count))
else @.Allocation
end
where AccountType = @.AccountType
Terri wrote:
> I'd like to build a stored procedure that would allocate a single value
> among records. If there was a rounded remainder I would like to randomly
> pick one of the records to receive the "extra" rounded amount. I'd like to
> pass my procedure 2 variables: @.AccountType and @.Amount. The amount
> allocated should always be to 2 decimal places.
> If the variables were @.AccountType = "A' and @.Amount = 10 the AccountValue
> for both AccountIDs 1 and 2 would be 5.00
> If the variables were @.AccountType = "A' and @.Amount = 11 the AccountValue
> for both AccountIDs 1 and 2 would be 5.50
> If the variables were @.AccountType = "B' and @.Amount = 10 the AccountValue
> would be 3.33 for 2 of the accounts and 3.34 for the third account. I'd li
ke
> to randomize which account gets 3.34, the extra penny.
> Can anyone help me write this stored procedure?
> CREATE TABLE [dbo].[Accounts] (
> [AccountID] [int] NOT NULL ,
> [AccountType] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [AccountValue] [decimal](18, 2) NULL
> ) ON [PRIMARY]
> GO
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (1,'A',NULL)
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (2,'A',NULL)
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (3,'B',NULL)
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (4,'B',NULL)
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (5,'B',NULL)
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (6,'C',NULL)
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (7,'C',NULL)
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (8,'C',NULL)
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (9,'C',NULL)
> INSERT INTO Accounts (AccountID,AccountType,AccountValue) VALUES
> (10,'C',NULL)
>|||Great, thanks, works perfectly. I have a related allocation method that I
need a stored procedure for so I am going to continue this thread and hope
for the continued expertise and generosity of this group.
I've modified the structure of my table to include an additional field,
Assets. In this method I want to allocate based on the assets of an account.
If I was allocating to 10.00 to all Accounts of AccountType 'A' and the
assets of AccountID 1 is 90.00 and AccountID 2 had assets of 10.00, Account
1 would be allocated 9.00 and account 2 would get 1.00.
Logically, add the assets of all AccountTypes 'A' (100.00) and determine
each accounts percentage of the total. Account 1 has 90% of total and
Account 2 has 10% of total, then allocate based on these percentages.
In this method I don't want to assign the rounded amount randomly but
instead based on "assets". If the number is rounded up it should be
allocated to the account with the most assets. If the number is allocated
down it should go to the account with the least assets.
My actual asset figures are in the millions so ties would be extremely
unlikely.
Sample data and expected results for AccountType 'B'; Amount to be
allocated: 100.00
AccountID, AccountType,Assets,Expected Result
3,'B',33.35,17.33
4,'B',85.01,44.19
5,'B',74.02,38.48
Thanks to anyone who might help.
DROP TABLE Accounts
CREATE TABLE [dbo].[Accounts] (
[AccountID] [int] NOT NULL ,
[AccountType] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[AccountValue] [decimal](18, 2) NULL,
[Assets] [decimal](18, 2) NULL,
) ON [PRIMARY]
GO
INSERT INTO Accounts (AccountID,AccountType,AccountValue,Asse
ts) VALUES
(1,'A',NULL,90)
INSERT INTO Accounts (AccountID,AccountType,AccountValue,Asse
ts) VALUES
(2,'A',NULL,10)
INSERT INTO Accounts (AccountID,AccountType,AccountValue,Asse
ts) VALUES
(3,'B',NULL,33.35)
INSERT INTO Accounts (AccountID,AccountType,AccountValue,Asse
ts) VALUES
(4,'B',NULL,85.01)
INSERT INTO Accounts (AccountID,AccountType,AccountValue,Asse
ts) VALUES
(5,'B',NULL,74.02)
"Trey Walpole" <treypole@.newsgroups.nospam> wrote in message
news:#3xiFQEAGHA.2656@.tk2msftngp13.phx.gbl...
> e.g.
> alter procedure AllocateToAccounts(@.AccountType char(1), @.TotalValue
> decimal(18,2))
> as
> declare
> @.Allocation decimal(18,2),
> @.Count int, @.RandID int
> select @.Count = count(*)
> from Accounts
> where AccountType = @.AccountType
> select @.RandID = (
> select top 1 AccountID
> from Accounts
> where AccountType=@.AccountType
> order by NewID()
> )
> select @.Allocation = @.TotalValue / @.Count
> update Accounts
> set AccountValue =
> case AccountID
> when @.RandID then @.Allocation + (@.TotalValue-(@.Allocation*@.Count))
> else @.Allocation
> end
> where AccountType = @.AccountType
>

Sunday, February 12, 2012

All tables under partitioned union view get locked - how to reduce?

Hi. We are using a partitioned view across a number of tables. They are
partitioned on a single column. We use the view to insert and update rows in
the underlying tables.
When loading all the rows for table XYZ through the view we find that SQL
Server is creating IX locks against all of the tables even though all the
rows are for one table only as identified by the check constraint on the
partitioned column.
Is there any way to get SQL Server to lock only the table that will be
affected?
McGy
[url]http://mcgy.blogspot.com[/url]"McGy" <anon@.anon.com> wrote in message
news:eIsUGsjhGHA.4368@.TK2MSFTNGP03.phx.gbl...
> Hi. We are using a partitioned view across a number of tables. They are
> partitioned on a single column. We use the view to insert and update rows
> in the underlying tables.
> When loading all the rows for table XYZ through the view we find that SQL
> Server is creating IX locks against all of the tables even though all the
> rows are for one table only as identified by the check constraint on the
> partitioned column.
> Is there any way to get SQL Server to lock only the table that will be
> affected?
>
Partitioned views have serious limitations. You should expect to have to
directly address the underlying tables for many operations. Qu|||Cheers David. I am considering duplicating the load stored procedures, 1 per
table, so as to remove the dependency on the partitioned view. It will make
maintenance a bit more complex but ultimately performance should improve.
Does that sound sensible?
Thanks.
McGy
[url]http://mcgy.blogspot.com[/url]
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:O4TVf3khGHA.3860@.TK2MSFTNGP02.phx.gbl...
> "McGy" <anon@.anon.com> wrote in message
> news:eIsUGsjhGHA.4368@.TK2MSFTNGP03.phx.gbl...
rows
SQL
the
> Partitioned views have serious limitations. You should expect to have to
> directly address the underlying tables for many operations. Qu
>|||"McGy" <anon@.anon.com> wrote in message
news:%23AUMJNvhGHA.4252@.TK2MSFTNGP04.phx.gbl...
> Cheers David. I am considering duplicating the load stored procedures, 1
> per
> table, so as to remove the dependency on the partitioned view. It will
> make
> maintenance a bit more complex but ultimately performance should improve.
> Does that sound sensible?
>
Yes. And you can always use dynamic SQL to load the table.
David|||The only problem with dynamic SQL is that the stored procedure would be a
nightmare to maintain in that form as it uses lots of variables. Also,
because of the stored procedure's size it takes several seconds to compile.
Presumably we would take that compilation hit each time the dynamic SQL is
constructed and prepared for execution?
McGy
[url]http://mcgy.blogspot.com[/url]
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:#cVWYyzhGHA.1612@.TK2MSFTNGP04.phx.gbl...
> "McGy" <anon@.anon.com> wrote in message
> news:%23AUMJNvhGHA.4252@.TK2MSFTNGP04.phx.gbl...
improve.
> Yes. And you can always use dynamic SQL to load the table.
> David
>|||Not necessarily; if you use sp_executeSQL with variables you are
essentially creating a parameterized SQL statement which increases the
likelihood that the execution plan will be reused.
This issue is interesting to me; we use partioned views as well.
However all of our inserts are done with bulk load methods, so locking
hasn't been a problem (yet). I understand the SQL 2005's partitoned
tables are much better than the the views; yet another reason to
consider upgrading.
Stu
McGy wrote:
> The only problem with dynamic SQL is that the stored procedure would be a
> nightmare to maintain in that form as it uses lots of variables. Also,
> because of the stored procedure's size it takes several seconds to compile
.
> Presumably we would take that compilation hit each time the dynamic SQL is
> constructed and prepared for execution?
> --
> McGy
> [url]http://mcgy.blogspot.com[/url]
>
> "David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
> message news:#cVWYyzhGHA.1612@.TK2MSFTNGP04.phx.gbl...
> improve.|||Hmmm. Interesting point on the sp_executesql. I'll take a look.
We use bulk insert too; but we also need to apply a bunch of rules to the
records that get loaded. They get loaded twice: once in to a transaction
table which are all inserts - the other in to an aggregation table that
applies certain rules depending upon what type the transaction is.
Just thought of a problem on the dynamic SQL front. The stored procedure
would be well in excess of the 8000 char limit on variable sizes. Is there a
way to work around that?
McGy
[url]http://mcgy.blogspot.com[/url]
"Stu" <stuart.ainsworth@.gmail.com> wrote in message
news:1149427023.769413.5440@.i39g2000cwa.googlegroups.com...
> Not necessarily; if you use sp_executeSQL with variables you are
> essentially creating a parameterized SQL statement which increases the
> likelihood that the execution plan will be reused.
> This issue is interesting to me; we use partioned views as well.
> However all of our inserts are done with bulk load methods, so locking
> hasn't been a problem (yet). I understand the SQL 2005's partitoned
> tables are much better than the the views; yet another reason to
> consider upgrading.
> Stu
> McGy wrote:
a
compile.
is
procedures, 1
will
>|||McGy (anon@.anon.com) writes:
> Hi. We are using a partitioned view across a number of tables. They are
> partitioned on a single column. We use the view to insert and update
> rows in the underlying tables.
> When loading all the rows for table XYZ through the view we find that SQL
> Server is creating IX locks against all of the tables even though all the
> rows are for one table only as identified by the check constraint on the
> partitioned column.
> Is there any way to get SQL Server to lock only the table that will be
> affected?
Are the intent locks causing any real problems? I ran a quick test, and
I was not able detect any locking problems, but I might have missed
something. (I was only testing concurrent SELECT statements.)
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Hi Erland. I am not actually sure if those IX locks are causing a problem.
What is the impact of an IX lock? When we were loading multiple files in
production we noticed that some were being blocked waiting for others to
complete. We assumed it was because of the view creating locks against all
the tables - but perhaps not?
McGy
[url]http://mcgy.blogspot.com[/url]
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97D9732464F2Yazorman@.127.0.0.1...
> McGy (anon@.anon.com) writes:
> Are the intent locks causing any real problems? I ran a quick test, and
> I was not able detect any locking problems, but I might have missed
> something. (I was only testing concurrent SELECT statements.)
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||McGy (anon@.anon.com) writes:
> Hi Erland. I am not actually sure if those IX locks are causing a problem.
> What is the impact of an IX lock? When we were loading multiple files in
> production we noticed that some were being blocked waiting for others to
> complete. We assumed it was because of the view creating locks against all
> the tables - but perhaps not?
The purpose of an intent lock is to tell "I am working here, so don't
try to take all this place for your own".
If a transaction updates a few rows in a table it acquires an X lock
on these rows, and also an IX lock on the table. This prevents other
processes from getting an X-lock on the table. Or put into other words,
the process that wants an exclusive lock on the table, does not need to
check if any rows are currently locked.
In case of the partitioned view the IX locks are there to prevent other
processes from acquire exclusive locks on the other tables, as there
could suddenly appear a row that should be inserted the other tables.
SQL Server cannot conclude that all data that is being inserted goes
only into one table in the view.
So, yes, if you have different insert processes in parallel, they will
block each other. And a process what would try "SELECT COUNT(*) FROM
pview" or anything else that requires a scan would also probably be
blocked, even with a condition that filtered out the table being
loaded.
If you want to run parallel loads at maximum speeds, you will probably
have to load into the underlying tables directly.
I will have to admit that I had read up in Books Online on what an
intent lock really is.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

All rows in a Sigle page

Hi guys,
I have requirement to show all the data from a table in a single page
or minimum 100 rows in the first page . How it is possible. Please any
body can solve my query.
Thanks in advance.
ShrinivasYou can go to report properties and adjust the page height in layout
tab.
Shrinivas wrote:
> Hi guys,
>
> I have requirement to show all the data from a table in a single page
> or minimum 100 rows in the first page . How it is possible. Please any
> body can solve my query.
> Thanks in advance.
> Shrinivas

Thursday, February 9, 2012

All it takes is one noise word to ruin query!

The presence of a single noise word is causing query to fail with this
error: "Execution of a full-text operation failed. A clause of the query
contained only ignored words." But the full text clause contains three
words and only one of them is a noise word. Please see below. Is this a
known problem?
Thanks
Timo
QUERY ONE EXCERPT:
This succeeds:
declare @.keywordclause varchar(2000)
set @.keywordclause='FORMSOF(INFLECTIONAL,disease) AND
FORMSOF(INFLECTIONAL,risk)'
...
{select statement}
where contains(mycolumn,@.keywordclause) or contains(myothercolumn,
@.keywordclause)
QUERY TWO EXCERPT
This fails:
declare @.keywordclause varchar(2000)
set @.keywordclause= 'FORMSOF(INFLECTIONAL,disease) AND
FORMSOF(INFLECTIONAL,risk) AND FORMSOF(INFLECTIONAL,your)'
...
where contains(mycolumn, @.keywordclause) or contains(myothercolumn,
@.keywordclause)
Timo,
Unfortunately, yes this is a well known and a "by design" issue. There are
several workarounds, one is to remove all noise words (except for a single
space) from your language-specific noise file (US English = noise.enu) and
run a full population. Another is to encapsulate your search strings in
double quotes as this will allow the noise word to be truly ignored. For
more information on this see, SQL 2000 BOL topic "Full-text Search
Recommendations" and KB article 246800 (Q246800) "INF: Correctly Parsing
Quotation Marks in FTS Queries" at
http://support.microsoft.com//defaul...b;EN-US;246800 for more
details.
Thanks,
John
SQL Full Text Search Blog
http://spaces.msn.com/members/jtkane/
"Timo" <Timo@.unspam.biz> wrote in message
news:#hK$w7EcFHA.1568@.TK2MSFTNGP10.phx.gbl...
> The presence of a single noise word is causing query to fail with this
> error: "Execution of a full-text operation failed. A clause of the query
> contained only ignored words." But the full text clause contains three
> words and only one of them is a noise word. Please see below. Is this a
> known problem?
> Thanks
> Timo
>
> QUERY ONE EXCERPT:
> This succeeds:
> declare @.keywordclause varchar(2000)
> set @.keywordclause='FORMSOF(INFLECTIONAL,disease) AND
> FORMSOF(INFLECTIONAL,risk)'
> ...
> {select statement}
> where contains(mycolumn,@.keywordclause) or contains(myothercolumn,
> @.keywordclause)
> QUERY TWO EXCERPT
> This fails:
> declare @.keywordclause varchar(2000)
> set @.keywordclause= 'FORMSOF(INFLECTIONAL,disease) AND
> FORMSOF(INFLECTIONAL,risk) AND FORMSOF(INFLECTIONAL,your)'
> ...
> where contains(mycolumn, @.keywordclause) or contains(myothercolumn,
> @.keywordclause)
>
|||Thanks for the reply, John, but I don't understand yet. Perhaps I'm
misreading the KB article, or it could be that the KB article does not
reflect the MSFT code base; it reads:
"Notice that proximity, generation, and weighted terms do not use double
quotation marks."
Here's the algorithm excerpted verbatim from the article:
Replace all double quotes (clears the text and any improper quotations)
If the text string contains one of the key words "NEAR", "FORMSOF", or
"ISABOUT", the parsing is complete
Else
Surround any instances of 'and' or 'and not' with quotes
Surround any instances of 'or' or 'or not' with quotes
Surround the entire string with quotesI'm using the FORMSOF generation
function:
declare @.keywordclause varchar(2000)
set @.keywordclause='FORMSOF(INFLECTIONAL,disease) AND
FORMSOF(INFLECTIONAL,risk)'
So I *shouldn't* be using double-quotation marks, right?
My workaround has been to create my own noisewords table and eliminate from
@.keywordclause any term that appears in my noisewords table.
Regards
Timo
"John Kane" <jt-kane@.comcast.net> wrote in message
news:#qdAANIcFHA.2768@.tk2msftngp13.phx.gbl...
> Timo,
> Unfortunately, yes this is a well known and a "by design" issue. There are
> several workarounds, one is to remove all noise words (except for a single
> space) from your language-specific noise file (US English = noise.enu) and
> run a full population. Another is to encapsulate your search strings in
> double quotes as this will allow the noise word to be truly ignored. For
> more information on this see, SQL 2000 BOL topic "Full-text Search
> Recommendations" and KB article 246800 (Q246800) "INF: Correctly Parsing
> Quotation Marks in FTS Queries" at
> http://support.microsoft.com//defaul...b;EN-US;246800 for more
> details.
> Thanks,
> John
> --
> SQL Full Text Search Blog
> http://spaces.msn.com/members/jtkane/
>
> "Timo" <Timo@.unspam.biz> wrote in message
> news:#hK$w7EcFHA.1568@.TK2MSFTNGP10.phx.gbl...
>
|||Timo,
Both single and double quotes are useful depending upon your requirements.
As for double quotes, I was referring to the following in "Full-text Search
Recommendations":
You also may encounter Error 7619, "The query contained only ignored words"
when using any of the full-text predicates in a full-text query, such as
CONTAINS(pr_info, 'between AND king'). The word "between" is an ignored or
noise word and the full-text query parser considers this an error, even with
an OR clause. Consider rewriting this query to a phrase-based query, ...
Specifically, note the different results from the above re-written query:
select * from pub_info where CONTAINS(pr_info, 'between AND king')
-- returns: Execution of a full-text operation failed. A clause of the query
contained only ignored words.
select * from pub_info where CONTAINS(pr_info, '"between AND king"')
-- returns: 0 rows as king is not in the pub_info table, and no error.
Your solution also works as there are many means to the same solution!
Thanks,
John
SQL Full Text Search Blog
http://spaces.msn.com/members/jtkane/
"Timo" <Timo@.spamspamspam.org> wrote in message
news:#q2sV2McFHA.720@.TK2MSFTNGP15.phx.gbl...
> Thanks for the reply, John, but I don't understand yet. Perhaps I'm
> misreading the KB article, or it could be that the KB article does not
> reflect the MSFT code base; it reads:
> "Notice that proximity, generation, and weighted terms do not use double
> quotation marks."
> Here's the algorithm excerpted verbatim from the article:
> Replace all double quotes (clears the text and any improper quotations)
> If the text string contains one of the key words "NEAR", "FORMSOF", or
> "ISABOUT", the parsing is complete
> Else
> Surround any instances of 'and' or 'and not' with quotes
> Surround any instances of 'or' or 'or not' with quotes
> Surround the entire string with quotesI'm using the FORMSOF
generation
> function:
> declare @.keywordclause varchar(2000)
> set @.keywordclause='FORMSOF(INFLECTIONAL,disease) AND
> FORMSOF(INFLECTIONAL,risk)'
> So I *shouldn't* be using double-quotation marks, right?
> My workaround has been to create my own noisewords table and eliminate
from[vbcol=seagreen]
> @.keywordclause any term that appears in my noisewords table.
> Regards
> Timo
>
> "John Kane" <jt-kane@.comcast.net> wrote in message
> news:#qdAANIcFHA.2768@.tk2msftngp13.phx.gbl...
are[vbcol=seagreen]
single[vbcol=seagreen]
and[vbcol=seagreen]
query[vbcol=seagreen]
three[vbcol=seagreen]
a
>
|||you might want to check out
http://www.indexserverfaq.com/noise.htm
The forms of inflectional search is functionally equivalent to a freetext
search. You might get what you are looking for using FreeText search and
this query won't bomb if a noise word is in your search condition.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Timo" <Timo@.unspam.biz> wrote in message
news:%23hK$w7EcFHA.1568@.TK2MSFTNGP10.phx.gbl...
> The presence of a single noise word is causing query to fail with this
> error: "Execution of a full-text operation failed. A clause of the query
> contained only ignored words." But the full text clause contains three
> words and only one of them is a noise word. Please see below. Is this a
> known problem?
> Thanks
> Timo
>
> QUERY ONE EXCERPT:
> This succeeds:
> declare @.keywordclause varchar(2000)
> set @.keywordclause='FORMSOF(INFLECTIONAL,disease) AND
> FORMSOF(INFLECTIONAL,risk)'
> ...
> {select statement}
> where contains(mycolumn,@.keywordclause) or contains(myothercolumn,
> @.keywordclause)
> QUERY TWO EXCERPT
> This fails:
> declare @.keywordclause varchar(2000)
> set @.keywordclause= 'FORMSOF(INFLECTIONAL,disease) AND
> FORMSOF(INFLECTIONAL,risk) AND FORMSOF(INFLECTIONAL,your)'
> ...
> where contains(mycolumn, @.keywordclause) or contains(myothercolumn,
> @.keywordclause)
>
|||Thanks for the link, Hilary. We've opted to strip the noiswords out of the
search argument using TSQL with own noisewords table. So far, the parsing
doesn't seem to be taxing the CPU. Perhaps the page you sent me to could add
a YMMV disclaimer instead of panning this approach in such absolute terms
;-)
Timo
"Hilary Cotter" <hilary.cotter@.gmail.com> wrote in message
news:ejECfeQcFHA.228@.TK2MSFTNGP12.phx.gbl...
> you might want to check out
> http://www.indexserverfaq.com/noise.htm
> The forms of inflectional search is functionally equivalent to a freetext
> search. You might get what you are looking for using FreeText search and
> this query won't bomb if a noise word is in your search condition.
> --
> Hilary Cotter
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
|||Your point is noted! Its not an application breaker, but you will get
better performance by doing this client side.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Timo" <Timo@.spamspamspam.org> wrote in message
news:O1IZpgacFHA.1152@.tk2msftngp13.phx.gbl...
> Thanks for the link, Hilary. We've opted to strip the noiswords out of
> the
> search argument using TSQL with own noisewords table. So far, the parsing
> doesn't seem to be taxing the CPU. Perhaps the page you sent me to could
> add
> a YMMV disclaimer instead of panning this approach in such absolute terms
> ;-)
> Timo
>
> "Hilary Cotter" <hilary.cotter@.gmail.com> wrote in message
> news:ejECfeQcFHA.228@.TK2MSFTNGP12.phx.gbl...
>

All drivers in ODBC MMC console are missing

When I open up the ODBC MMC console on this Win XP computer and click on the
Drivers tab, there is not a single driver listed in the tab.
I then tried to install latest MDAC and confirmed through Component Checker
utility that SQL related drivers are installed in the computer, but yet thes
e
drivers dont appear in the ODBC MMC console. I've even tried to reinstall Je
t
4.0 for Win XP but have the same result.
Any idea how do I get back the list in the Drivers tab of ODBC MMC console?Hi
You may want to try un-registering/registering each of dlls with regsvr32,
or check out the articles regarding removing MDAC on MSDN and then re-instal
l
it.
John
"Teo Chee Yang" wrote:
[vbcol=seagreen]
> When I open up the ODBC MMC console on this Win XP computer and click on t
he
> Drivers tab, there is not a single driver listed in the tab.
> I then tried to install latest MDAC and confirmed through Component Checke
r
> utility that SQL related drivers are installed in the computer, but yet th
ese
> drivers dont appear in the ODBC MMC console. I've even tried to reinstall
Jet
> 4.0 for Win XP but have the same result.
> Any idea how do I get back the list in the Drivers tab of ODBC MMC console?[/vbcol
]

All drivers in ODBC MMC console are missing

When I open up the ODBC MMC console on this Win XP computer and click on the
Drivers tab, there is not a single driver listed in the tab.
I then tried to install latest MDAC and confirmed through Component Checker
utility that SQL related drivers are installed in the computer, but yet these
drivers dont appear in the ODBC MMC console. I've even tried to reinstall Jet
4.0 for Win XP but have the same result.
Any idea how do I get back the list in the Drivers tab of ODBC MMC console?
Hi
You may want to try un-registering/registering each of dlls with regsvr32,
or check out the articles regarding removing MDAC on MSDN and then re-install
it.
John
"Teo Chee Yang" wrote:

> When I open up the ODBC MMC console on this Win XP computer and click on the
> Drivers tab, there is not a single driver listed in the tab.
> I then tried to install latest MDAC and confirmed through Component Checker
> utility that SQL related drivers are installed in the computer, but yet these
> drivers dont appear in the ODBC MMC console. I've even tried to reinstall Jet
> 4.0 for Win XP but have the same result.
> Any idea how do I get back the list in the Drivers tab of ODBC MMC console?

All drivers in ODBC MMC console are missing

When I open up the ODBC MMC console on this Win XP computer and click on the
Drivers tab, there is not a single driver listed in the tab.
I then tried to install latest MDAC and confirmed through Component Checker
utility that SQL related drivers are installed in the computer, but yet these
drivers dont appear in the ODBC MMC console. I've even tried to reinstall Jet
4.0 for Win XP but have the same result.
Any idea how do I get back the list in the Drivers tab of ODBC MMC console?Hi
You may want to try un-registering/registering each of dlls with regsvr32,
or check out the articles regarding removing MDAC on MSDN and then re-install
it.
John
"Teo Chee Yang" wrote:
> When I open up the ODBC MMC console on this Win XP computer and click on the
> Drivers tab, there is not a single driver listed in the tab.
> I then tried to install latest MDAC and confirmed through Component Checker
> utility that SQL related drivers are installed in the computer, but yet these
> drivers dont appear in the ODBC MMC console. I've even tried to reinstall Jet
> 4.0 for Win XP but have the same result.
> Any idea how do I get back the list in the Drivers tab of ODBC MMC console?