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 |
|
JKCarter
Starting Member
4 Posts |
Posted - 2005-12-14 : 18:16:51
|
| Hope this makes sense.I am trying to convert an Access based blog app to SQL Server but I'm having some trouble with some SQL.The sql is as follows:SELECT *, (SELECT COUNT(*) FROM tblComment WHERE tblComment.blogID = tblBlog.BlogID AND tblComment.commentInclude <> 0) AS TOTAL_LINKSFROM joinBlogWHERE BlogIncluded <> 0ORDER BY BlogID DESCThe access version returns blog entries & the number of comments posted to each entry.joinBlog is an Access query:SELECT tblBlog.BlogID, tblBlog.CatID AS tblBlog_CatID, tblBlog.BlogHeadline, tblBlog.BlogHTML, tblBlog.BlogDate, tblBlog.BlogIncluded, tblCategory.catID AS tblCategory_catID, tblCategory.catNameFROM tblCategory RIGHT JOIN tblBlog ON tblCategory.catID = tblBlog.CatID;I assume I need to make a view out of the Access query, I have done this & that appears to work.The problem I have is when I try the 1st sql that is in my page with sql server I get the following error:The column prefix 'tblBlog' does not match with a table name or alias name used in the query.I can make the following change which returns data but does not attach the blog comment counts to the proper blog entry, instead it returns the total comments in the query:SELECT *, (SELECT COUNT(*) FROM tblComment,tblBlog WHERE tblComment.blogID = tblBlog.BlogID AND tblComment.commentInclude <> 0) AS TOTAL_LINKSFROM joinBlogWHERE BlogIncluded <> 0ORDER BY BlogID DESCCan anyone tell me how to convert this for SQL Server? This is my 1st access to sql server attempt.Thanks. |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
JKCarter
Starting Member
4 Posts |
Posted - 2005-12-19 : 12:04:58
|
quote: Originally posted by X002548 Welcome to SQLTEAMPlease red the link in my sig. And Post What it asks for thereAre you using stored procs?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam
No Stored Procedures, just a view joinBlogThanks. if exists (select * from dbo.sysobjects where id = object_id(N'[jcarterfb].[FK_tblBlog_tblCategory]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)ALTER TABLE [jcarterfb].[tblBlog] DROP CONSTRAINT FK_tblBlog_tblCategoryGOif exists (select * from dbo.sysobjects where id = object_id(N'[jcarterfb].[tblBlog]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [jcarterfb].[tblBlog]GOif exists (select * from dbo.sysobjects where id = object_id(N'[jcarterfb].[tblComment]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [jcarterfb].[tblComment]GOif exists (select * from dbo.sysobjects where id = object_id(N'[jcarterfb].[tblCategory]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [jcarterfb].[tblCategory]GOCREATE TABLE [jcarterfb].[tblComment] ( [commentID] [int] IDENTITY (1, 1) NOT NULL , [blogID] [int] NOT NULL , [commentDate] [smalldatetime] NOT NULL , [commentName] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [commentEmail] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [commentURL] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [commentHTML] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [commentInclude] [bit] NOT NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GOCREATE TABLE [jcarterfb].[tblCategory] ( [catID] [int] IDENTITY (1, 1) NOT NULL , [catName] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GOCREATE TABLE [jcarterfb].[tblBlog] ( [BlogID] [int] IDENTITY (1, 1) NOT NULL , [CatID] [int] NOT NULL , [BlogHeadline] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [BlogHTML] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [BlogDate] [smalldatetime] NOT NULL , [BlogIncluded] [bit] NOT NULL , [File] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GOALTER TABLE [jcarterfb].[tblComment] WITH NOCHECK ADD CONSTRAINT [PK_tblComment] PRIMARY KEY CLUSTERED ( [commentID] ) ON [PRIMARY] GOALTER TABLE [jcarterfb].[tblCategory] WITH NOCHECK ADD CONSTRAINT [PK_tblCategory] PRIMARY KEY CLUSTERED ( [catID] ) ON [PRIMARY] GOALTER TABLE [jcarterfb].[tblBlog] WITH NOCHECK ADD CONSTRAINT [PK_tblBlog] PRIMARY KEY CLUSTERED ( [BlogID] ) ON [PRIMARY] GOALTER TABLE [jcarterfb].[tblBlog] ADD CONSTRAINT [FK_tblBlog_tblCategory] FOREIGN KEY ( [CatID] ) REFERENCES [jcarterfb].[tblCategory] ( [catID] ) NOT FOR REPLICATION GOalter table [jcarterfb].[tblBlog] nocheck constraint [FK_tblBlog_tblCategory]GO |
 |
|
|
TonyTheDBA
Posting Yak Master
121 Posts |
Posted - 2005-12-19 : 14:22:25
|
| Not sure if this is relevant but when you prefix your objects with an owner, unless you specifically call them like that then they do not exist for another user.Eg your object [jcarterfb].[tblBlog] cannot be seen by user James in the followingSELECT * FROM tblBlogBut will exist if he usesSELECT * FROM [jcarterfb].[tblBlog]If you want all users to have access to the objects in your database then you need to use the prefix dbo.object for exampleCreate Table dbo.Employees ( EmployeeID Integer , FirstName Varchar (50) , LastName Varchar (50) )Will be visible to all users either as SELECT * FROM EmployeesORSELECT * FROM dbo.EmployeesHope this helps-- RegardsTony The DBA |
 |
|
|
|
|
|
|
|