Showing posts with label dynamic. Show all posts
Showing posts with label dynamic. Show all posts

Tuesday, March 27, 2012

Calling Stored Procedure from a stored procedure on where clause (URGENT!)

I need a stored procedure that include dynamic where clause. so I
created these two sp. GetCampaigns calls Checkvalues sp to complete
where clauseb but it doesn't work. any idea ?
CREATE PROCEDURE GetCampaigns (@.CampaignTypeId int,@.CardTypeId int)
AS
SELECT Campaigns.* FROM Campaigns
WHERE (CheckValues 'Campaigns.CampaignTypesId',@.CampaignTypeId) AND
(CheckValues 'Campaigns.CardTypesId',@.CardTypeId)
GO
CREATE PROCEDURE CheckValues(@.InitialStr nvarchar(30),@.Type int)
AS
IF((@.Type = 1) OR (@.Type = 0))
SELECT @.InitialStr + '!=0'
ELSE
SELECT @.InitialStr + '=' + @.Type
GOUse a function and not a procedure for CheckValues
Daniel EYER
"ONEY" <ozcankanbur@.gmail.com> a crit dans le message de
news:c16976dd.0502220317.735fa507@.posting.google.com...
> I need a stored procedure that include dynamic where clause. so I
> created these two sp. GetCampaigns calls Checkvalues sp to complete
> where clauseb but it doesn't work. any idea ?
>
> CREATE PROCEDURE GetCampaigns (@.CampaignTypeId int,@.CardTypeId int)
> AS
> SELECT Campaigns.* FROM Campaigns
> WHERE (CheckValues 'Campaigns.CampaignTypesId',@.CampaignTypeId) AND
> (CheckValues 'Campaigns.CardTypesId',@.CardTypeId)
> GO
> CREATE PROCEDURE CheckValues(@.InitialStr nvarchar(30),@.Type int)
> AS
> IF((@.Type = 1) OR (@.Type = 0))
> SELECT @.InitialStr + '!=0'
> ELSE
> SELECT @.InitialStr + '=' + @.Type
> GO|||Oney
Don't use dynamic WHERE condition ,instead read this great article
http://www.sommarskog.se/dyn-search.html
"Daniel Eyer" <daniel.eyer@.free.fr> wrote in message
news:421b2391$0$1226$8fcfb975@.news.wanadoo.fr...
> Use a function and not a procedure for CheckValues
> Daniel EYER
> "ONEY" <ozcankanbur@.gmail.com> a crit dans le message de
> news:c16976dd.0502220317.735fa507@.posting.google.com...
>sql

Monday, March 19, 2012

Calling All Dynamic SQL Gods!

Hello
I'm desperate for help with the following dynamic SQL. It used to work for
ages but suddenly stopped working today! I can't recall changing anything of
importance.. So I say. Anyway, I'm getting this error: "Cannot use empty
object or column names. Use a single space if necessary."
I've identified the location within the script that causes this message it's
this line:
(i2b_vw_key.KeyCode + CASE ManagementSet WHEN 1 THEN " MG" ELSE "" END +
CASE Master WHEN 1 THEN " MA" ELSE "" END) AS KeyCode,
3rd line of Set @.cmdSQL =.

I've been trying to insert a single space between "" which eliminates half
of the error but I can't figure out what quotes to use around 'MG' and 'MA'.

I'd be grateful if you can have a look at this and let me know how to
correct this problem.
Like I said it used to work and I'm perplexed about this sudden error. Is
there any change that can cause this behaviour?

Many thanks for your efforts!!
Have a nice day!!

Martin

Paging Script:

CREATE PROCEDURE dbo.sp_ListKeyOut(
@.page_number INT,
@.number_of_records INT,
@.cmdWHERE VARCHAR(200),
@.cmdORDERBY VARCHAR(200)
) AS
SET NOCOUNT ON

DECLARE
@.SizeString VARCHAR(5),
@.PrevString VARCHAR(5),
@.cmdSQL varchar(2000)

SET @.SizeString = CONVERT(VARCHAR, @.number_of_records)
SET @.PrevString = CONVERT(VARCHAR, @.number_of_records * (@.page_number - 1))

SET QUOTED_IDENTIFIER OFF

SET @.cmdSQL = 'SELECT COALESCE((i2b_vw_contact.Firstname + CHAR(32) +
i2b_vw_contact.Lastname),i2b_vw_company.CompanyNam e) AS CName,
i2b_vw_keytransactionlog.KeyTransactionLogID,
i2b_vw_keytransactionlog.KeyID,
(i2b_vw_key.KeyCode + CASE ManagementSet WHEN 1 THEN " MG" ELSE "" END +
CASE Master WHEN 1 THEN " MA" ELSE "" END) AS KeyCode,
i2b_vw_address.Address1 AS PropertyAddress, i2b_vw_contact.MobileNo,
A.ProgUserName AS ProgUserName,
CONVERT (varchar(10), i2b_vw_keytransactionlog.TransactionDate, 104 ) AS
TransactionDate,
CONVERT(varchar(10),i2b_vw_keytransactionlog.Retur nByDate,104) AS
ReturnByDate
FROM i2b_vw_keytransactionlog
LEFT JOIN i2b_vw_contact ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_contact.ContactID AND i2b_vw_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_vw_company ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_company.CompanyID AND i2b_vw_keytransactionlog.IsContact = 0)
JOIN i2b_vw_key ON (i2b_vw_keytransactionlog.KeyID = i2b_vw_key.KeyID)
JOIN i2b_vw_property on (i2b_vw_key.PropertyID = i2b_vw_property.PropertyID)
JOIN i2b_vw_address ON (i2b_vw_property.AddressID =
i2b_vw_address.AddressID)
JOIN i2b_vw_proguser AS A ON (i2b_vw_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE KeyTransactionLogID IN'

IF @.cmdWHERE IS NULL OR @.cmdWHERE = ''
BEGIN

EXEC(
@.cmdSQL +
'(SELECT TOP ' + @.SizeString + ' KeyTransactionLogID FROM
i2b_vw_keytransactionlog
LEFT JOIN i2b_vw_contact ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_contact.ContactID AND i2b_vw_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_vw_company ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_company.CompanyID AND i2b_vw_keytransactionlog.IsContact = 0)
JOIN i2b_vw_key ON (i2b_vw_keytransactionlog.KeyID = i2b_vw_key.KeyID)
JOIN i2b_vw_property on (i2b_vw_key.PropertyID = i2b_vw_property.PropertyID)
JOIN i2b_vw_address ON (i2b_vw_property.AddressID =
i2b_vw_address.AddressID)
JOIN i2b_vw_proguser AS A ON (i2b_vw_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE KeyTransactionLogID NOT IN
(SELECT TOP ' + @.PrevString + ' KeyTransactionLogID FROM
i2b_vw_keytransactionlog
LEFT JOIN i2b_vw_contact ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_contact.ContactID AND i2b_vw_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_vw_company ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_company.CompanyID AND i2b_vw_keytransactionlog.IsContact = 0)
JOIN i2b_vw_key ON (i2b_vw_keytransactionlog.KeyID = i2b_vw_key.KeyID)
JOIN i2b_vw_property on (i2b_vw_key.PropertyID = i2b_vw_property.PropertyID)
JOIN i2b_vw_address ON (i2b_vw_property.AddressID =
i2b_vw_address.AddressID)
JOIN i2b_vw_proguser AS A ON (i2b_vw_keytransactionlog.ProgUserID =
A.ProgUserID)
ORDER BY ' + @.cmdORDERBY + ')
ORDER BY ' + @.cmdORDERBY + ') ORDER BY ' + @.cmdORDERBY
)

-- EXEC('SELECT (COUNT(*) - 1)/' + @.SizeString + ' + 1 AS PageCount FROM
i2b_vw_keytransactionlog')

END
ELSE
BEGIN

EXEC(
@.cmdSQL +
'(SELECT TOP ' + @.SizeString + ' KeyTransactionLogID FROM
i2b_vw_keytransactionlog
LEFT JOIN i2b_vw_contact ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_contact.ContactID AND i2b_vw_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_vw_company ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_company.CompanyID AND i2b_vw_keytransactionlog.IsContact = 0)
JOIN i2b_vw_key ON (i2b_vw_keytransactionlog.KeyID = i2b_vw_key.KeyID)
JOIN i2b_vw_property on (i2b_vw_key.PropertyID = i2b_vw_property.PropertyID)
JOIN i2b_vw_address ON (i2b_vw_property.AddressID =
i2b_vw_address.AddressID)
JOIN i2b_vw_proguser AS A ON (i2b_vw_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE ' + @.cmdWHERE + ' AND KeyTransactionLogID NOT IN
(SELECT TOP ' + @.PrevString + ' KeyTransactionLogID FROM
i2b_vw_keytransactionlog
LEFT JOIN i2b_vw_contact ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_contact.ContactID AND i2b_vw_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_vw_company ON (i2b_vw_keytransactionlog.EntityID =
i2b_vw_company.CompanyID AND i2b_vw_keytransactionlog.IsContact = 0)
JOIN i2b_vw_key ON (i2b_vw_keytransactionlog.KeyID = i2b_vw_key.KeyID)
JOIN i2b_vw_property on (i2b_vw_key.PropertyID = i2b_vw_property.PropertyID)
JOIN i2b_vw_address ON (i2b_vw_property.AddressID =
i2b_vw_address.AddressID)
JOIN i2b_vw_proguser AS A ON (i2b_vw_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE ' + @.cmdWHERE + ' ORDER BY ' + @.cmdORDERBY + ')
ORDER BY ' + @.cmdORDERBY + ') ORDER BY ' + @.cmdORDERBY
)

-- EXEC('SELECT (COUNT(*) - 1)/' + @.SizeString + ' + 1 AS PageCount FROM
i2b_vw_keytransactionlog WHERE ' + @.cmdWHERE)

END

SET QUOTED_IDENTIFIER ON

RETURN 0
GOMartin Feuersteiner (theintrepidfox@.hotmail.com) writes:
> I'm desperate for help with the following dynamic SQL. It used to work
> for ages but suddenly stopped working today! I can't recall changing
> anything of importance.. So I say. Anyway, I'm getting this error:
> "Cannot use empty object or column names. Use a single space if
> necessary."

In T-SQL there are two ways to delimit a string literal: '' and "". ANSI
SQL permits only '', and reserves "" for quoting identifiers with
"funny" characters, so you can have column names like "order date".

Of this reason, SQL Server offers a setting QUOTED_IDENTIFIER that can
be on or off. If ON, you can only use '' to delimit string literal, if
off, you can use both '' and "".

There is functionality (indexed views, indexed computed columns) in SQL
Server that is only available if QUOTED_IDENTIFIER is ON, so stick with this
setting. This is also the default setting with many client libraries.
Unfortunately, Enterprise Manager has the bad habit of setting it off, and
since this setting is saved with the procedure this can cause confusinon at
times.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Hi Erland

I knew you would go for it :-)
Well, I know that stuff with '' and ", my problem is that I can't
comprehend how many of ' I need to use e.g. on each site of 'MG' to get
it working. It causes a brain overflow!! Can you please provide me with
a hands-on example perhaps using my code?

(i2b_vw_key.KeyCode + CASE ManagementSet WHEN 1 THEN " MG" ELSE "" END +
CASE Master WHEN 1 THEN " MA" ELSE "" END) AS KeyCode,

Many thanks for your efforts!
Have a nice day!

Martin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||Martin Ashcroft (theintrepidfox@.hotmail.com) writes:
> I knew you would go for it :-)
> Well, I know that stuff with '' and ", my problem is that I can't
> comprehend how many of ' I need to use e.g. on each site of 'MG' to get
> it working. It causes a brain overflow!! Can you please provide me with
> a hands-on example perhaps using my code?
> (i2b_vw_key.KeyCode + CASE ManagementSet WHEN 1 THEN " MG" ELSE "" END +
> CASE Master WHEN 1 THEN " MA" ELSE "" END) AS KeyCode,

Check out http://www.sommarskog.se/dynamic_sq...#good_practices
for some tips.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thank you Erland!
It helped!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Saturday, February 25, 2012

Call a stored procedure from another SP?

Hello,

I'm trying to get a dynamic Web log report out of SQL Server 2000. I am sending all of my IIS activity to a SQL Server table called inetlog. I need to get a summary of downloads for particular files, and within a certain time period. So, I start by getting unique file names from the table by building a view:

CREATE VIEW UniqueFileNames
AS
SELECT TOP 100 PERCENT COUNT (t.target) AS NumDownloads,
t.target

FROM inetlog t

GROUP BY t.target
ORDER BY t.NumDownloads DESC

GO

Now that I've got this I can run my stored procedure which passes a number representing the days within which the downloads occurred:

CREATE PROCEDURE sp_DownloadsWithinNumDays

@.NumDays INT

AS

SELECT u.NumDownloads / 3 AS Downloads,
LEFT(u.Target,(LEN(u.Target)-4)) AS DocName,
UPPER(RIGHT((RTRIM(u.Target)),3)) AS DocType,
CONVERT(CHAR(8), MAX(i.logtime), 1) AS LogDate

FROM UniqueFileNames u

INNER JOIN
inetlog i
ON u.target = i.target

WHERE DATEDIFF(DAY, i.logtime, GETDATE()) <= @.NumDays AND
(u.Target LIKE '%.pdf%' OR
u.Target LIKE '%.doc%' OR
u.Target LIKE '%.zip%')

GROUP BY
u.target,
u.NumDownloads

ORDER BY Downloads DESC

GO

This works just fine - I pipe my results to a Web page and I get a list of *.doc, *.pdf, and *.zip files downloaded within the past x days, including a count. The problem is that the count never changes. If I put 10 days in the SP, I get a list of files downloaded within the last 10 days, but the count shows ALL downloads of that file. If I put 5 days, I get the file downloads from the past 5 days, with the same counts (i.e., all downloads).

I know why this is happening: it's because the view that I join to is the count of all downloads, but what I can't figure out is how to dynamically generate this count so that the input parameter for the SP (num days) is used.

Sorry for the long post, but I'm getting myself confused, so I thought I'd be very explicit. Thanks for any help you can offer.

Dylan
dylan_thomas@.esri.comMy understanding:

You want the count of downloads in the last X days for each file downloaded in the last X days...

Forget the view and try this proc.... unless I missed something...

CREATE PROCEDURE sp_DownloadsWithinNumDays

@.NumDays INT

AS

DECLARE
@.var_FromDate as datetime

SET NOCOUNT ON

SET @.var_FromDate = DATEADD ( d , 0 - @.NumDays, GETDATE() )

SELECT COUNT(*) AS Downloads,
LEFT(i.Target,(LEN(i.Target)-4)) AS DocName,
UPPER(RIGHT((RTRIM(i.Target)),3)) AS DocType,
CONVERT(CHAR(8), MAX(i.logtime), 1) AS LogDate

FROM inetlog i

WHERE i.logtime > @.var_FromDate AND
(i.Target LIKE '%.pdf%' OR
i.Target LIKE '%.doc%' OR
i.Target LIKE '%.zip%')

GROUP BY
i.target

ORDER BY 1 DESC

GO|||Andy - Fantastic! Runs faster, and gives me the result that I need. I'm very new to SQL Server (if you couldn't tell!), so it's good to see how you've used a variable in this procedure. Opens up a whole new world of possibilities for a SQL-based doc management system that I'm working on.

Thanks :-)|||CREATE PROCEDURE sp_DownloadsWithinNumDays
@.NumDays INT
AS
SELECT
u.NumDownloads / 3 AS Downloads
,LEFT(u.Target,(LEN(u.Target)-4)) AS DocName
,UPPER(RIGHT((RTRIM(u.Target)),3)) AS DocType
,CONVERT(CHAR(8), MAX(i.logtime), 1) AS LogDate
FROM
(
SELECT Target,COUNT(Target) AS NumDownloads
FROM inetlog
WHERE DATEDIFF(DAY, logtime, GETDATE()) <= @.NumDays AND
(
Target LIKE '%.pdf%' OR
Target LIKE '%.doc%' OR
Target LIKE '%.zip%'
)
GROUP BY Target
) u
INNER JOIN inetlog i ON u.target = i.target
WHERE DATEDIFF(DAY, i.logtime, GETDATE()) <= @.NumDays AND
(
i.Target LIKE '%.pdf%' OR
i.Target LIKE '%.doc%' OR
i.Target LIKE '%.zip%'
)
ORDER BY Downloads DESC, Target ASC|||No worries!