Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2008-09-11 : 23:06:45
|
| I have five db's in this example and need to return the unique siteid etc from each DB. The following code is returning all four siteid when I select any of the DB in @DBName. The first three siteid shown below are in all DB's and the fourth is only in the first db.USE [VC]GODECLARE @return_value intEXEC @return_value = [dbo].[Sites_with_no_Events_AD] @DBName = N'vca1'SELECT 'Return Value' = @return_value1760 Alix FastVU3213 James FastTX1764 Jazz FastVU3238 VC No Events FastTraceBecause in this case I have selected VCA1 as the @DBName I should only get the first three from DB 2. If I select VC as the DBName I should get all four. The AND does not seem to be working in the where clause, where is my syntax incorrect please?Declare @sql nvarchar(max)Select @sql=''Select @sql=@sql+ 'SELECT SiteDetails.SiteID, SiteDetails.SiteName, SiteDetails.TransmitterTypeFROM SiteDetails CROSS JOINEventDetailsWHERE '''+name+''' IN (Select Param From fn_MVParam ('''+@DBName+''','','')) AND (NOT (SiteDetails.SiteID IN (SELECT SiteID FROM EventDetails))) ' from sys.databases where name='VC' or name like 'VCA%' |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-09-11 : 23:54:10
|
There seems to be a lot of issues.1. Probably not a good spot to use Dynamic Sql2. Query does not appear to be written properly from a optimization standpoint3. Looks like the wrong methodolgy.4. Query is returning the wrong resultsLet's start with a clean slate, and please post some sample data and desired results (Please also include some sample data for your table function fn_MVParam as well. This way we can fully test.Thanks Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2008-09-12 : 00:12:44
|
| I have five DB called VC, VCA1, VCA2, VCA3, VCA4 in this example. (this could be 1 to x).The function is a parser for the multivalued parameter from reporting services.I have two tables called SiteDetails and EventDetails (the same in each DB). SiteDetails has SiteID as key and EventDetails has SiteID as a field. I am tring to return the SiteDetails.SiteId where there are no matching EventDetails.SiteId. Ie the site(s) has no events.The data in the databases where the sitedetails.siteid has no matching eventdetails.siteid is as follows, typically for most sitedetails.siteid it will have one or more linked eventdetails.siteid:VCSiteID SiteName TransmitterType1760 Alix FastVU3213 James FastTX1764 Jazz FastVU3238 VC No Events FastTraceVCA1 to VCA4SiteID SiteName TransmitterType1760 Alix FastVU3213 James FastTX1764 Jazz FastVUThe only input parameter is the database name as @DBName, at the moment I am running the store proc from management studio to test but will use the stored proc finally from reporting services.The desired result is:@DBName = VCSiteID SiteName TransmitterType1760 Alix FastVU3213 James FastTX1764 Jazz FastVU3238 VC No Events FastTrace@DBName = VCA1SiteID SiteName TransmitterType1760 Alix FastVU3213 James FastTX1764 Jazz FastVU@DBName = VC, VCA1SiteID SiteName TransmitterType1760 Alix FastVU3213 James FastTX1764 Jazz FastVU3238 VC No Events FastTraceUSE [VC]GO/****** Object: Table [dbo].[SiteDetails] Script Date: 09/12/2008 15:10:29 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[SiteDetails]( [SiteID] [int] IDENTITY(1,1) NOT NULL, [DeviceID] [nvarchar](50) NOT NULL, [SiteName] [nvarchar](50) NOT NULL, [TransmitterType] [nvarchar](50) NOT NULL, [SaveLiveToDB] [bit] NOT NULL, [StayOnlinePriority] [int] NOT NULL, [DownloadImages] [bit] NOT NULL, [ImageDwnldStrategy] [nvarchar](20) NULL, [SitePulseEnable] [bit] NOT NULL, [NotifyIfSiteNotHeardFrom] [int] NULL, [AllOKPassword] [nvarchar](50) NOT NULL, [NotificationUserField] [nvarchar](max) NULL, [ContactUserField] [nvarchar](max) NULL, [LocationUserField] [nvarchar](max) NULL, [ContactCompany] [nvarchar](255) NULL, [ContactName] [nvarchar](255) NULL, [ContactPhone] [nvarchar](255) NULL, [ContactFax] [nvarchar](255) NULL, [ContactMobile] [nvarchar](255) NULL, [ContactEmail] [nvarchar](max) NULL, [LocationAddress] [nvarchar](max) NULL, [LocationCity] [nvarchar](max) NULL, [LocationState] [nvarchar](max) NULL, [LocationZip] [nvarchar](max) NULL, [LocationMapReference] [nvarchar](max) NULL, [LocationDescription] [nvarchar](max) NULL, [LocationTimeZone] [nvarchar](20) NULL, [InteractiveDiagramFileName] [nvarchar](max) NULL, [Isdeleted] [bit] NOT NULLGO/****** Object: Table [dbo].[EventDetails] Script Date: 09/12/2008 15:10:08 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[EventDetails]( [EventID] [int] IDENTITY(1,1) NOT NULL, [CameraNumber] [int] NULL, [EventType] [nvarchar](50) NOT NULL, [Description] [nvarchar](max) NULL, [Priority] [int] NOT NULL, [State] [nvarchar](50) NOT NULL, [Cause] [nvarchar](max) NULL, [SourceID] [int] NULL, [SourceType] [nvarchar](255) NULL, [ACKFlag] [tinyint] NOT NULL CONSTRAINT [DF_EventDetails_ACKFlag] DEFAULT ((0)), [FullyACKed] [bit] NOT NULL, [IsInCurrentDB] [bit] NOT NULL CONSTRAINT [DF_EventDetails_IsInCurrentDB] DEFAULT ((1)), [EventStartTime] [datetime] NOT NULL, [EventEndTime] [datetime] NULL, [SiteID] [int] NOT NULL, [StateType] [nvarchar](50) NULL, [StateData] [varbinary](max) NULLHere is a print of the current query execution, I cannot see why the where clause is not working for the parameter?SELECT SiteDetails.SiteID, SiteDetails.SiteName, SiteDetails.TransmitterTypeFROM SiteDetails CROSS JOINEventDetailsWHERE 'VC' IN ('vca1',',') AND(NOT (SiteDetails.SiteID IN (SELECT SiteID FROM EventDetails))) SELECT SiteDetails.SiteID, SiteDetails.SiteName, SiteDetails.TransmitterTypeFROM SiteDetails CROSS JOINEventDetailsWHERE 'VCA1' IN ('vca1',',') AND(NOT (SiteDetails.SiteID IN (SELECT SiteID FROM EventDetails))) SELECT SiteDetails.SiteID, SiteDetails.SiteName, SiteDetails.TransmitterTypeFROM SiteDetails CROSS JOINEventDetailsWHERE 'VCA2' IN ('vca1',',') AND(NOT (SiteDetails.SiteID IN (SELECT SiteID FROM EventDetails))) SELECT SiteDetails.SiteID, SiteDetails.SiteName, SiteDetails.TransmitterTypeFROM SiteDetails CROSS JOINEventDetailsWHERE 'VCA3' IN ('vca1',',') AND(NOT (SiteDetails.SiteID IN (SELECT SiteID FROM EventDetails))) SELECT SiteDetails.SiteID, SiteDetails.SiteName, SiteDetails.TransmitterTypeFROM SiteDetails CROSS JOINEventDetailsWHERE 'VCA4' IN ('vca1',',') AND(NOT (SiteDetails.SiteID IN (SELECT SiteID FROM EventDetails))) (1 row(s) affected)Hopefully that is everything? |
 |
|
|
|
|
|
|
|