|
Hi,
I tried to apply for update like below in SP.
Error:
Server: Msg 16957, Level 16, State 4, Procedure BicRank, Line 12
FOR UPDATE cannot be specified on a READ ONLY cursor.
SP:
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure dbo.BicRank Script Date: 08/01/2008 12:44:52 ******/
ALTER PROCEDURE BicRank
AS
-- determine mo3 bic and m03 ranking for mtc dmoq's ('TTR_1', 'TTR_2', 'TTR_3', 'PROGRS')
-- ADDED ('TTR_6', 'TTR_12', 'TTR_24') By Nirmala 02/09/2007
--rankings are completed per dmoq, service, bicuniverse combination
--100% is considered best
Declare BicRank_DESC Cursor
for Select dmoq + service + bicuniverse as compare, mo3_percent from mtce_toexcel
where (Mo3_Percent < 101) and dmoq in ('TTR_1','TTR_2','TTR_3','TTR_6','TTR_12','TTR_24','PROGRS')
order by compare, Mo3_percent desc
for update of mo3_bic,mo3_rank
declare @3mo as float, @rank as int, @heldcompare as varchar(60), @bic as float, @firstbic as float, @compare as varchar(60)
declare @dmoq as varchar(20), @service as varchar(20), @bicuniverse as varchar(6), @RCD_CNT INTEGER
OPEN BicRank_DESC
set @heldcompare = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
FETCH NEXT FROM bicrank_DESC into @compare, @3mo
while (@@fetch_status <> -1)
begin
IF (@heldcompare <> @compare)
begin
set @rank = 1 set @bic = @3mo set @firstbic = @3mo set @heldcompare = @compare
end
ELSE IF (@3mo <> @bic)
begin
set @rank = @rank + 1 set @bic = @3mo
end
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRANSACTION
Update MTce_toExcel set Mo3_bic = @firstbic, Mo3_rank = @rank where current of BicRank_DESC
FETCH NEXT FROM bicrank_DESC into @compare, @3mo
END
commit transaction
CLOSE BicRank_DESC
DEALLOCATE BicRank_DESC
--determine mo3 bic and m03 ranking for mtc dmoq's ('FAIL_FREQ', 'NRF', 'MTTR','MTTR_3')
--rankings are completed per dmoq, service, bicuniverse combination
--0% is considered best
Declare BicRank_ASC Cursor
for Select dmoq + service + bicuniverse as compare, mo3_percent from mtce_toexcel
where ( Mo3_Percent < 101 AND MO3_PERCENT IS NOT NULL) and dmoq in ('FAIL_FREQ', 'NRF', 'MTTR','MTTR_3','MTTR_F')
order by compare, Mo3_percent asc
for update of mo3_bic,mo3_rank
OPEN BicRank_ASC
set @heldcompare = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
FETCH NEXT FROM bicrank_ASC into @compare, @3mo
while (@@fetch_status <> -1)
begin
IF @heldcompare <> @compare
begin
set @rank = 1 set @bic = @3mo set @firstbic = @3mo set @heldcompare = @compare
end
ELSE IF (@3mo <> @bic)
begin
set @rank = @rank + 1 set @bic = @3mo
end
Update MTce_toExcel set Mo3_bic = @firstbic, Mo3_rank = @rank where current of BicRank_ASC
FETCH NEXT FROM bicrank_ASC into @compare, @3mo
END
CLOSE BicRank_ASC
DEALLOCATE BicRank_ASC
--determineYTD bic andYTD ranking for mtc dmoq's ('FAIL_FREQ', 'NRF', 'MTTR','MTTR_3')
--rankings are completed per dmoq, service, bicuniverse combination
--0% is considered best
Declare BicRank_YTD_DESC Cursor
for Select dmoq + service + bicuniverse as compare, YTD_percent from mtce_toexcel
where ( YTD_Percent < 101 AND YTD_PERCENT IS NOT NULL) and dmoq in ('FAIL_FREQ', 'NRF', 'MTTR','MTTR_3','MTTR_F')
order by compare, YTD_percent asc
for update of ytd_bic,ytd_rank
declare @YTD float
OPEN BicRank_YTD_DESC
set @heldcompare = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
FETCH NEXT FROM bicrank_YTD_DESC into @compare, @YTD
while (@@fetch_status <> -1)
begin
IF @heldcompare <> @compare
begin
set @rank = 1 set @bic = @YTD set @firstbic = @YTD set @heldcompare = @compare
end
ELSE IF (@YTD <> @bic)
begin
set @rank = @rank + 1 set @bic = @YTD
end
Update MTce_toExcel set YTD_bic = @firstbic,YTD_rank = @rank where current of BicRank_YTD_DESC
FETCH NEXT FROM bicrank_YTD_DESC into @compare, @YTD
END
CLOSE BicRank_YTD_DESC
DEALLOCATE BicRank_YTD_DESC
--determineYTD bic andYTD ranking for mtc dmoq's ('TTR_1', 'TTR_2', 'TTR_3', 'PROGRS')
--rankings are completed per dmoq, service, bicuniverse combination
--100% is considered best
-- ADDED ('TTR_6', 'TTR_12', 'TTR_24') By Nirmala 02/09/2007
Declare BicRank_YTD_ASC Cursor
for Select dmoq + service + bicuniverse as compare, YTD_percent from mtce_toexcel
where (YTD_Percent < 101 ) and dmoq in ('TTR_1', 'TTR_2', 'TTR_3','TTR_6','TTR_12','TTR_24','PROGRS')
order by compare, YTD_percent desc
for update of ytd_bic,ytd_rank
OPEN BicRank_YTD_ASC
set @heldcompare = 'xxxxxxxxxxxxxxxxxxxxxxx'
FETCH NEXT FROM bicrank_YTD_ASC into @compare, @YTD
while (@@fetch_status <> -1)
begin
IF @heldcompare <> @compare
begin
set @rank = 1 set @bic = @YTD set @firstbic = @YTD set @heldcompare = @compare
end
ELSE IF (@YTD <> @bic)
begin
set @rank = @rank + 1 set @bic = @YTD
end
Update MTce_toExcel set YTD_bic = @firstbic,YTD_rank = @rank where current of BicRank_YTD_ASC
FETCH NEXT FROM bicrank_YTD_ASC into @compare, @YTD
END
CLOSE BicRank_YTD_ASC
DEALLOCATE BicRank_YTD_ASC
--
--Populate MO3_BIC on records where MO3_BIC is null
--This is due to the MO3_PERCENT being 101, 102 or NULL
--
Declare BicRank_null Cursor
for Select distinct dmoq, service, bicuniverse from mtce_toexcel where mo3_bic is null
OPEN BicRank_null
FETCH NEXT FROM bicrank_null into @dmoq, @service, @bicuniverse
while (@@fetch_status <> -1)
begin
Declare
mo3_bic Cursor For
Select DISTINCT MO3_BIC from mtce_toexcel where mo3_bic is NOT null and
dmoq = @dmoq And service = @service and bicuniverse = @bicuniverse
SET @RCD_CNT = @@ROWCOUNT
-- get the mo3_bic
Open mo3_bic
Fetch Next From mo3_bic Into @FIRSTBIC
if @RCD_CNT > 0
BEGIN
UPdate MTce_toExcel set Mo3_bic = @firstbic where mo3_bic is null and
dmoq = @dmoq And service = @service and bicuniverse = @bicuniverse
END
Close mo3_bic
Deallocate mo3_bic
FETCH NEXT FROM bicrank_null into @dmoq, @service, @bicuniverse
END
CLOSE BicRank_null
DEALLOCATE BicRank_null
--
--Populate YTD_BIC on records where YTD_BIC is null
--This is due to the YTD_PERCENT being 101, 102 or NULL
--
Declare BicRank_null_ytd Cursor
for Select distinct dmoq, service, bicuniverse from mtce_toexcel where YTD_bic is null
OPEN BicRank_null_ytd
FETCH NEXT FROM bicrank_null_ytd into @dmoq, @service, @bicuniverse
while (@@fetch_status <> -1)
begin
Declare
YTD_bic Cursor For
Select distinct ytd_bic from mtce_toexcel where YTD_bic is not null and
dmoq = @dmoq And service = @service and bicuniverse = @bicuniverse
SET @RCD_CNT = @@ROWCOUNT
-- get theYTD_bic
Open YTD_bic
Fetch Next From YTD_bic Into @firstbic
if @RCD_CNT > 0
BEGIN
Update MTce_toExcel set YTD_bic = @firstbic where YTD_bic is null and
dmoq = @dmoq And service = @service and bicuniverse = @bicuniverse
END
Close YTD_bic
Deallocate YTD_bic
FETCH NEXT FROM bicrank_null_ytd into @dmoq, @service, @bicuniverse
END
CLOSE BicRank_null_ytd
DEALLOCATE BicRank_null_ytd
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
|
|
|
|
|
Hello All,
Our server poopped the bed and we got it back to crawling slowly. We had some small but slightly important databases on the server which we are trying to reattach. We keep receiving an error indicating the log file is corrupted in some manner, therefore we are trying to reattach the mdf file without the ldf. Again we are running into issues.
Has anyone ever successfully reattached a database without the LDF file?
Any responses will be greatly appreciated.
|
|
|
|
|
The way SQL Server works is that when you make a change to the database, the operation is immediately logged to the log file. It then makes the changes to the in-memory copy of the data, but does not immediately flush those changes back to the data file. It only does so when it needs to flush data out to reuse the buffer space, on idle, and when a checkpoint operation occurs. The checkpoint operation basically synchronizes the data and log, and is done frequently enough that the configured recovery interval can be achieved, that is, the time to recover from a system failure assuming that the data and log files are intact. The default recover interval is one minute, that is, a recovery should take around one minute to complete.
(In practice the log is also buffered to allow multiple log records to be written out in one operation. However, when you commit a transaction, the log records relating to that transaction have to be completely written to disk before SQL Server will proceed with the following statement or return to the client.)
The upshot of this lazy writing is that the data file may not be transactionally consistent. In fact, it probably isn't. Your best bet is to restore from a known-good backup - you will of course lose the operations that took place after the backup was made.
I'm not sure if you can reattach this database. What you probably should have done is run DBCC CHECKDB with one of the REPAIR options. You may be able to attach using CREATE DATABASE ... FOR ATTACH_REBUILD_LOG on SQL Server 2005; for SQL Server 2000 try sp_attach_db.
For the future, if you're not already doing so: it's considered best practice to keep your transaction logs on a separate disk (array) from the data, so that a failed data volume doesn't cause you to lose your transaction logs as well - remember that if you have logs but no data file you can back up the logs, restore a full database backup then restore the log backup to recover to the point of failure. In addition you can get some performance benefit because the disk head is typically over the place that the next block of log records need to be written - disk latency is governed by head seek time. If you plan to use RAID to protect your logs, go with RAID 1, mirroring - there is no benefit from striping (the writes are sequential and would simply target each disk in turn) and RAID 5 hurts write performance.
DoEvents: Generating unexpected recursion since 1991
|
|
|
|
|
I know MS-SQL can connect to other databases to communicate to them with Linked Servers. Does anyone know if Sybase can do this? If so, how is this accomplished? Thank you in advance.
modified on Tuesday, January 22, 2008 9:49:42 AM
|
|
|
|
|
Hi,
I have an sp name Movie_SP which returns all the movies by taking MovieName
Now in my New sp(Search_Movies_SP) I want to use the out put of the first SP(Movie_SP)
and with the result of Movie_SP I have to manipulate
Actually I need to set priorities as my first SP Movie_SP is returning all the movies with say BLACK in an Improper way
I want to make it to a order where Black comes First,
then The Movies which starts with Black second,
and the third is The Movies which contains Black
Plz help me
thanks
Pashi
|
|
|
|
|
Could've been better if you have mentioned which database you are working with.Assuming that you are working with Sql Server...
pashitech wrote: use the out put of the first SP
Are you looking out for something like an Output Parameter[^]?
If no (which I assume so), I guess you'll have to populate the data onto a temporary table for manipulations.
pashitech wrote: make it to a order
You may try using the LIKE[^]operator.
Select * from tblMovies where MovieName = 'Black'
The above query fetches you details where the movie name is black (an exact match)
Select * from tblMovies where MovieName Like 'Black%'
The above query gives you details where the movie name begins with black (including black)
Select * from tblMovies where MovieName like '%Black%'
This one fetches movie names that contain the word black.
-- For better usage with the like operator, refer to the URL linked.
|
|
|
|
|
How about:
create table #temp1 (
--list of columns returned by Movie_SP goes here
)
insert into #temp1
exec Movie_SP @Param1, Param2, ...
select * from #temp1
order by case
when Title = 'Black' then 'A'
when Title LIKE 'Black %' then 'B'
else 'C'
end,
Title
You can use "insert into" to return the resultset of a stored procedure into a temporary table. The following select statement uses a case statement to prioritorise the ordering. You might need to tweak this a bit because I didn't quite understand your requirements.
Regards
Andy
|
|
|
|
|
I need to create a User Defined Type for time. I tried rewriting my database tables as shown below and I get the error that is shown below.
Do I need to do something else to create them? Do I need to use hh:mm for the formatting of the datapart I need to use?
All I need in ts_from and ts_to is the hour and the minutes.
I am using SQL Server Express 2005.
create table tbl_teach_sched (<br />
ts_id int NOT NULL IDENTITY(1000,1),<br />
teach_id int,<br />
ts_day text,<br />
ts_datetime datetime,<br />
ts_from as (datepart(hh:mm, ts_datetime)) persisted,<br />
ts_to as (datepart(hh:mm, ts_datetime)) persisted,<br />
primary key(ts_id),<br />
foreign key(teach_id) references tbl_teachers(teach_id));
This is the error.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 'hh:'.
Msg 132, Level 15, State 1, Line 8
The label 'hh' has already been declared. Label names must be unique within a query batch or stored procedure.
|
|
|
|
|
Yeah, you can't do that.
Either just use the DateTime, or use a smallint.
The smallint could be the number of minutes or seconds from midnight, or it could be hours*100+minutes.
But I spend so much time converting between time-storage schemes that I prefer DateTime; the savings in maintenance makes up for the waste of space.
|
|
|
|
|
I have a book taht suggests creating a user defined data type for time only? How is it that I can't create a user defined type for time alone?
|
|
|
|
|
I have Googled without success (I keep getting blog posts that deride Access/Jet SQL as 'sucking'* with a comment feature at the bottom of the page), but does anybody know how to code-comment Access/Jet SQL?
I'm attempting to show our resident VB "whiz" how he's left himself open to SQL Injection attacks - when his application targets SQL Server I can successfully hack his code in the manner described by various posts/articles by injecting my own SQL and by commenting out his, but in Access/Jet SQL it doesn't appear to work.
*Yes, I'm inclined to agree with the majority of these posts.
|
|
|
|
|
Hi,
I am new in the field of databases.
I am trying to create a strong typed dataset (using vs 2005 add data set wizard).
The table in my data base include foreign keys (relations).
When the wizard finishes the dataset is created with all the necessary tables and fields, EXCEPT for data relations.
When I am performing the same thing with access data base the relations are loaded well.
Does anyone have an idea of what it could be.
Sincerely yours
Y.R.
|
|
|
|
|
how would I select and count the number of values that match: j0107_a131 and show the following two values
j0107_a131
j0107_a131_01
the roomcode is in a string that is why i specified below 'like roomcode'
i.e. select count (room_code) from rooms where room_code like room_code
hope that makes sense cheers
|
|
|
|
|
Well, you could always do this:
select @room_code = @room_code + '%'
select count(room_code) from rooms where room_code like @room_code
|
|
|
|
|
i am using ASP.net, C# anD SQL Server2005,in the following query:
string q = "SELECT collapsed_building.b_name,collapsed_building.b_desc FROM collapsed_building WHERE collapsed_building.b_name LIKE '" + crimewithdate.text2 + "' ";
I WANT TO USE % AFTER THE KEYWORD like SO THAT ALL THE BUILDING NAMES WHICH MATCH THE VALUE ENTERED BY THE USER ARE DISPLAYED
WHEN I WRITE (LIKE '" + %crimewithdate.text2 %+ "'), IT GIVES ERROR, WHAT WILL BE THE CORRECT SENTAX
|
|
|
|
|
Please, don't do this. You leave yourself with open to a Sql Injection attack. Take a look at this[^] article on how to avoid this. Now, you many want to consider using something like this:
DECLARE @building_name NVARCHAR(100)
SELECT @building_name = 'Test'
-- The lines above are just a sample to get you started. Move this into a stored procedure and use
-- the code below to do the actual work. Note that you would want to determine whether or not
-- @building_name ended in a % before using this code.
SELECT @building_name = @building_name + '%'
SELECT collapsed_building.b_name,collapsed_building.b_desc FROM collapsed_building WHERE collapsed_building.b_name LIKE @building_name
|
|
|
|
|
Pete is correct in saying that this is dangerous code. You have to use strong
protection to make sure that crimewithdate.text2 does not contain
any malicious SQL code instead of the expected search parameter.
To answer your question as asked, here is one solution:
string q = string.Format("SELECT collapsed_building.b_name,
collapsed_building.b_desc FROM collapsed_building
WHERE collapsed_building.b_name LIKE '%{0}%'", crimewithdate.text2);
(The line breaks are for readability only)
Do realize that an evil user could enter something like;
x'; TRUNCATE TABLE collapsed_building; SELECT * FROM collapsed_building WHERE b_name LIKE 'x
and wreck your whole day.
It is much better to pass crimewithdate.text2 as a parameter and not expose your database to bad people.
modified on Tuesday, January 22, 2008 11:26:49 AM
|
|
|
|
|
Hi, i want to create a job.
http://msdn2.microsoft.com/en-us/library/ms191439.aspx[^]
By following this link, i did the same, but i unable to see the sqlserver agent in object explorer. i started the service in adminsitrative tools ->services. And also, i restarted the sqlserver. still not able to see sqlserver agent..can any one help regarding this.....
G. Satish
|
|
|
|
|
I have 2 Databases: in that, In the Database DB1, i write a stored procedure: In that i write
Select command for a the table that has to fetch from Database DB2.
How to write a Stored Procedure for this?
Regards,
LEE
|
|
|
|
|
If you are using SQL Server, identify your tables using the following naming convention:
databasename.owner.tablename
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Doing that sort of thing is generally a bad idea.
I have some code that does that and now we want to move one of the databases to a different server and can't without breaking the code. At best we could link the servers, but I'd still have to modify the code. (And we've had trouble with linked servers in the past.)
Plus, not all database systems allow that construct so it may lock you into SQL Server.
You may want to create a view that handles the cross-database query.
Anyway, be aware of the potential problems.
|
|
|
|
|
Hi freinds i want to develop crystal report which used different data source to dispaly different record in one singal report
There i have some Question
1 : Can i used Subreport for differnt datasource in main Crystal report and can i send parameter to them
2: can i useHide subreport i.e if i there is Condition to show subreport(a)
then all remain subreport shoul be hide mean to say shoe singal subreport in each case Can i do this Can some body tell me
wasim khan
|
|
|
|
|
|
Hi there,
Can someone tell me, is it possible in SQL Server 2005 how to Decrypt and Encrypted SP or View.
Mujtaba
"If both of us are having one apple each and we exchange it, at the end we both will have one apple each. BUT if both of us are having one idea each and we exchange it, at the end both of us will be having two ideas each."
|
|
|
|
|
The procedure encryption is actually referred to as obfuscation in Books Online. Also, there is no SQL Server command for decrypting it back. You could use third party components to accomplish this, there sure can be found some by searching in google for them.
The only feature for protecting code in SQL Server is the WITH ENCRYPTION clause (http://databases.aspfaq.com/database/how-do-i-protect-my-stored-procedure-code.html[^]). It is weak not necessarily because the encryption is weak (it uses RC4), but because the encryption key can be easily found. An attacker will focus on finding the encryption key rather than breaking the encryption algorithm in such a solution.
|
|
|
|
|