|
Thanks for such a carefully created answer - that's solved my problem perfectly and more importantly, helped me understand the issue I had.
Thanks again
|
|
|
|
|
You're welcome
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Hi,
I'm stuck getting info from all files in een directory using the filesystemobject.
Can someone help me out ?
here's my code so far :
DECLARE @fileobj INT , @fsobj INT
DECLARE @folderobj INT
DECLARE @exists INT, @error INT
DECLARE @Property varchar(255)
DECLARE @src VARCHAR(255), @desc VARCHAR(255)
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'Ole Automation Procedures', 1
RECONFIGURE
EXEC @error = sp_OACreate 'Scripting.FileSystemObject', @fsobj OUT
EXEC sp_OAGetErrorInfo @fsobj, @src OUT, @desc OUT
SELECT CONVERT(varbinary(4),@error), Source=@src, Description=@desc
-- EXEC sp_OAGetProperty @folderobj, 'Name', @Property OUT
-- print @Property
EXEC sp_OAMethod @fsobj, 'GetFolder', @folderobj OUT,'C:\'
EXEC sp_OAGetErrorInfo @folderobj, @src OUT, @desc OUT
SELECT CONVERT(varbinary(4),@error), Source=@src, Description=@desc
/* RETURN NOTHING */
--EXEC sp_OAGetProperty @folderobj, 'Name', @Property OUT
--print @Property
/* CREATE A TEMP TABLE */
IF OBJECT_ID('tempdb..#dirlist') IS NOT NULL
DROP TABLE #DirList
CREATE TABLE #dirlist (FName VARCHAR(1000))
/* FILL TEMP TABLE WITH FILES COLLECTION */
INSERT INTO #dirlist (FName)
EXEC sp_OAGetProperty @folderobj , 'Files', @fileobj OUTPUT
EXEC sp_OAGetErrorInfo @fileobj, @src OUT, @desc OUT
SELECT error=CONVERT(varbinary(4),@error), Source=@src, Description=@desc
/* SHOW FILES */
select * from #dirlist
Question is how can i fill my temp table? (it's still empty)
Thx,
Kurt
|
|
|
|
|
Hi All,,
I want to get a Max value from a table and increment it by 1,, such as
if the max value is 1/50, It will be 2/50, but the problem how to get it from a db.
thanks all,,
|
|
|
|
|
I'm not sure if I understood you correctly, but querying the maximum value for a column would be something like:
SELECT MAX(ColumnName) FROM TableName
Is that what you're looking for?
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
i din get ur problem in right way but i hope my answer will benefit u in some way.....
ex. if u hav a salary column in emp table and suppose if u want to select the maximum sal from table and increment it by 3000 rs. then try the following procedure for this...
create or replace procedure sal_increment as
v_maxsal number(20);
begin
select max(salary) into v_maxsal from emp;
update emp set salary=salary+3000 where salary=v_maxsal;
end;
Hope u understood the problem and the solution given above....
and i hope u had query in pl/sql.....
|
|
|
|
|
Hi all,
I made one procedure in which i use sp_cursoropen procedure and it has one parameter for rowcount. When i execute the procedure, it does not return rowcount, i mean it returns -1 even there is 20 records in my table.
Can anybody help me...
Thanks
Krishnraj
|
|
|
|
|
Hi
Put return at the end of you're stored procedure
declare @counter int
select @counter = count(*) from mytable
return @counter
or
set rowcount as output parameter and print it.
or
set nocount off
greetz
|
|
|
|
|
Hi topcatalpha,
Thanks for reply,
but sp_cursoropen has one parameter which returns the total count of table. so no need to fire extra query as u said.
sp_cursoropen is sql server 2000's in built system procedure
i think there may be some other settings.
any way thanks buddy...
Krishnraj
|
|
|
|
|
With what parameters are you callin this sp. It would depend on the cursor type if the rowcount can be returned. For example if the cursor is forward only, AFAIK the actual rowcount cannot be determined.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Hi Mika,
Below is my query where u find variable @mainQuery. which is my table fetch query with Inner join with 3 tables.
Declare @cursor int, @rowcount int
Exec sp_cursoropen @cursor output,@mainQuery,16,8196,@rowcount output
Select @rowcount
Exec sp_cursorfetch @cursor,32,@RowNumber,@PageSize
Exec sp_cursorclose @cursor
Do u think that there may be a problem with inner join query?
Thanks for replying...
Krishnraj
|
|
|
|
|
No I don't believe that the inner join is the problem. Now you're using fast forward-only cursor.
What happens if you change to keyset-driven cursor:
Exec sp_cursoropen @cursor output, @mainQuery, <code>1,</code> 8196, @rowcount output
or dynamic cursor
Exec sp_cursoropen @cursor output, @mainQuery, <code>2,</code> 8196, @rowcount output
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Hi Mika,
Yes your answer is correct, inner join is not problem here.
And as per u said, i used keyset-driven cursor then it gives me count..
So its briliant suggesion...
Thank you boss....
Krishnraj
|
|
|
|
|
You're welcome
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Hi All,
I accidently updated some data in Staff table on a production database, and i can't just simply install the back-up database as there are quite a few real time transaction occuring, is there a way that i can get the original staff data back? My database is M/S Sql Server 2000.
Many thanks.
|
|
|
|
|
You could restore the backup to another server and update the data back from the Staff-table on that restored database. This way you don't destroy other data in the production database.
For updating the data back from restored database, you can use for example SQL statements using linked server.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Oops it happens all the time, even to people who are very carefull,Everyone has work with SQL, has done something like that and you know what, If your Staff table was updated after the Back.There is no way you can recover those lost Updates. It happens to everyone remember that
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
I am having trouble with the Between condition and CASE expression in a stored procedure. I have set it up such that if a beginning date and end date are specified then return all records between the specified date range. If neither date is specified then I just return records that are between the actual date for records (basically Between 11/15/2008 and 11/15/2008). When I try to save the sp it gives me an "Incorrect Syntax near the keyword AND' error. I'm a newbie to using the CASE Statement so any help would be appreciated. Thanks in advance to all that respond.
--Case statements to determine which parameters to pass in
u.userCreationDate BETWEEN CASE WHEN @beginDate IS NULL
THEN u.userCreationDate AND u.userCreationDate
WHEN @endDate IS NULL THEN u.userCreationDate AND u.userCreationDate
ELSE @beginDate AND @endDate
END
|
|
|
|
|
Perhaps the easiest way to create the logic you're after would be using COALESCE funtion. Something like:
u.userCreationDate BETWEEN COALESCE(@beginDate, u.userCreationDate)
AND COALESCE(@endDate, u.userCreationDate)
If you want to use larger expression in BETWEEN operations you should use parenthesis like:
... BETWEEN (...) AND (...)
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
I used the COALESCE Function and it worked. Thanks Mike. Before closing, if you do not mind can you tell me exactly how that function works.
|
|
|
|
|
|
I need to solve the problem to execute sp_addextendedproperty with a new description for a column but that does in all the databases (more than 20) for typical the record created_id, deleted_id, updated_id….
I have proven it with sp_MSForeachDB and with a cursor for the tables nested within a cursor for databases but always it gives some problem with "USE @database".
This he would be the one that I believe that it would have to work but there is some detail that forgets to me.
Thank you very much, I have always worked with Oracle and I am new with Transact
USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_AAA_INSERT_DESCRIPTION_BY_SELECT_COLUMN]
@pCOLUMN VARCHAR(100),
@pDESCRIPTION VARCHAR(255),
@pForced smallint = 0,
@pBD VARCHAR(100) = NULL,
@pExceptBD VARCHAR(100) = NULL
/*
EXEC [SP_AAA_INSERT_DESCRIPTION_BY_SELECT_COLUMN] @pCOLUMN = 'CREATED_ID', @pDESCRIPTION = 'Record creation user', @pForced = 1
, @pBD = 'DBNAME'
, @pExceptBD = 'DBNAME'
*/
AS
BEGIN
SET NOCOUNT ON
DECLARE @cError INT
DECLARE @cErrorMsg VARCHAR(8000)
DECLARE @cSPName SYSNAME
DECLARE @cTIENE_TRAN INT
DECLARE @DB_Name varchar(100)
DECLARE @Command nvarchar(200)
DECLARE @strTable nvarchar(200)
DECLARE @strDescription nvarchar(2000)
SET @cError = 0
SET @cTIENE_TRAN = @@TRANCOUNT
SET @cSPname = OBJECT_NAME(@@PROCID)
BEGIN TRY
IF @cTIENE_TRAN = 0
BEGIN
SET TRANSACTION ISOLATION LEVEL SNAPSHOT
BEGIN TRANSACTION @cSPname
END
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
DECLARE crsBD CURSOR FOR
SELECT [Name]
FROM MASTER.sys.sysdatabases
WHERE [Name] NOT IN ( 'Master', 'tempdb', 'model', 'msdb' )
AND ( @pBD IS NULL OR [Name] = @pBD )
AND ( @pExceptBD IS NULL OR [Name] <> @pExceptBD )
ORDER BY [Name]
OPEN crsBD
FETCH NEXT FROM crsBD INTO @DB_Name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Command = 'USE [' + @DB_Name + ']'--+ CHAR(13) + CHAR(10) +' GO '+ CHAR(13) + CHAR(10)
print @Command
EXEC ( @Command )
BEGIN TRY
DECLARE crsTable CURSOR FOR
SELECT OBJECT_NAME(c.object_id) AS [TableName], Cast( ex.value AS nVarchar ) AS [Description]
FROM sys.columns c
LEFT OUTER JOIN sys.extended_properties ex
ON ex.major_id = c.object_id
AND ex.minor_id = c.column_id
WHERE OBJECTPROPERTY(c.object_id, 'IsMsShipped') = 0
AND ( ex.value is null OR ( @pForced = 1 AND ex.value is not null))
AND OBJECT_NAME(c.object_id) NOT LIKE 'VIW_%'
AND c.name = @pCOLUMN
OPEN crsTable
FETCH NEXT FROM crsTable INTO @strTable, @strDescription
WHILE @@FETCH_STATUS = 0
BEGIN
--print @strTable
IF ISNULL( @strDescription, '' ) = ''
BEGIN
SET @Command = 'sp_addextendedproperty ''MS_Description'' ,''' + @pDESCRIPTION + ''', ''user'', ''dbo'', ''table'', ''' + @strTable + ''', ''column'', '''+@pCOLUMN + ''''
END
ELSE
BEGIN
SET @Command = 'sp_updateextendedproperty ''MS_Description'' ,''' + @pDESCRIPTION + ''', ''user'', ''dbo'', ''table'', ''' + @strTable + ''', ''column'', '''+@pCOLUMN+''''
END
print @Command
-- EXEC sp_executesql @Command
EXEC ( @Command )
FETCH NEXT FROM crsTable INTO @strTable, @strDescription
END
CLOSE crsTable
DEALLOCATE crsTable
print '... ' + @DB_Name + ' (final)'
print ' '
END TRY
BEGIN CATCH
SELECT @cError = @@ERROR
DEALLOCATE crsTable
SET @cErrorMsg = dbo.fGET_ERROR()
RAISERROR(@cErrorMsg,16,1) WITH LOG
END CATCH
FETCH NEXT FROM crsBD INTO @DB_Name
END
CLOSE crsBD
DEALLOCATE crsBD
/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
IF @cTIENE_TRAN = 0 COMMIT TRANSACTION @cSPname
END TRY
BEGIN CATCH
SELECT @cError = @@ERROR
DEALLOCATE crsBD
IF @cTIENE_TRAN = 0 ROLLBACK TRANSACTION @cSPname
SET @cErrorMsg = dbo.fGET_ERROR()
RAISERROR(@cErrorMsg,16,1) WITH LOG
END CATCH
RETURN(@cERROR)
END
|
|
|
|
|
Didn't go through all the code, but at least it seems that you are using exec in parts. First when you change the database using exec, the change is valid only in the context where it's executed. Later on when you execute a procedure, the context is back in the original (master).
So you should build the execution string as a whole and then execute it. Something like:
@sqltext = 'USE CorrectDataBase; EXEC sp_...'
EXEC (@sqltext)
Hope this helps you forward,
Mika
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Hi, thanks for looking
I have a numeric column that i would like to reorder
I mean, there are gaps between the numbers, for example: 1,3,4,5,8,9
I would like it to be: 1,2,3,4,5,6
This column is the primarty key, i obiously have many rows
In other words, i want this column to have the value corresponsing to the row number
How could i accomplish this?
Thanks in advance
Alexei Rodriguez
|
|
|
|
|
Well, after thinking or a little bit, i found a solution using row_number() and a temp table
But i remember once i saw another way, it was a single query, but i dont remember where i saw it
If you know any other way, let meknow
Thanks
Alexei Rodriguez
|
|
|
|
|