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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Converting access app to sql server

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_LINKS
FROM joinBlog
WHERE BlogIncluded <> 0
ORDER BY BlogID DESC


The 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.catName
FROM 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_LINKS
FROM joinBlog
WHERE BlogIncluded <> 0
ORDER BY BlogID DESC


Can 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

Posted - 2005-12-14 : 21:06:27
Welcome to SQLTEAM

Please red the link in my sig. And Post What it asks for there

Are you using stored procs?





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

JKCarter
Starting Member

4 Posts

Posted - 2005-12-19 : 12:04:58
quote:
Originally posted by X002548

Welcome to SQLTEAM

Please red the link in my sig. And Post What it asks for there

Are you using stored procs?





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



No Stored Procedures, just a view joinBlog
Thanks.

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_tblCategory
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[jcarterfb].[tblBlog]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [jcarterfb].[tblBlog]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[jcarterfb].[tblComment]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [jcarterfb].[tblComment]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[jcarterfb].[tblCategory]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [jcarterfb].[tblCategory]
GO

CREATE 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]
GO

CREATE TABLE [jcarterfb].[tblCategory] (
[catID] [int] IDENTITY (1, 1) NOT NULL ,
[catName] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

CREATE 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]
GO

ALTER TABLE [jcarterfb].[tblComment] WITH NOCHECK ADD
CONSTRAINT [PK_tblComment] PRIMARY KEY CLUSTERED
(
[commentID]
) ON [PRIMARY]
GO

ALTER TABLE [jcarterfb].[tblCategory] WITH NOCHECK ADD
CONSTRAINT [PK_tblCategory] PRIMARY KEY CLUSTERED
(
[catID]
) ON [PRIMARY]
GO

ALTER TABLE [jcarterfb].[tblBlog] WITH NOCHECK ADD
CONSTRAINT [PK_tblBlog] PRIMARY KEY CLUSTERED
(
[BlogID]
) ON [PRIMARY]
GO

ALTER TABLE [jcarterfb].[tblBlog] ADD
CONSTRAINT [FK_tblBlog_tblCategory] FOREIGN KEY
(
[CatID]
) REFERENCES [jcarterfb].[tblCategory] (
[catID]
) NOT FOR REPLICATION
GO

alter table [jcarterfb].[tblBlog] nocheck constraint [FK_tblBlog_tblCategory]
GO


Go to Top of Page

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 following
SELECT * FROM tblBlog
But will exist if he uses
SELECT * 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 example

Create Table dbo.Employees (
EmployeeID Integer ,
FirstName Varchar (50) ,
LastName Varchar (50) )

Will be visible to all users either as

SELECT * FROM Employees
OR
SELECT * FROM dbo.Employees

Hope this helps


--
Regards
Tony The DBA
Go to Top of Page
   

- Advertisement -