![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Keep in mind this is my first compiled SQL program Stored Procedure (SP), copied from a book by Frasier Visual C++.NET in Visual Studio 2005 (Chap12). So far, so theory, except for one bug (feature?) below. At some point I'm sure I'll be able to laugh about this, akin to forgeting a semi-colon in C/C++, but right now it's frustrating (time to sleep on it for a while). Problem-- For some reason I get the error when trying to save files where two tables (called Author and Content), linked by a single key, form a relationship. By simple comparison of the source code in the textbook and my program (below) I found the difference: instead of, like in the textbook, the Stored Procedure (SP) starting with "CREATE PROCEDURE", it *automatically* is (was somehow) given the name of 'ALTER PROCEDURE' and I cannot change this to "CREATE PROCEDURE" (you get an error in MS Visual Studio 2005 Pro edition of "There is already an object named XXX in the database", see *|* below). No matter what I do, the SP is always changed by Visual Studio 2005 to 'ALTER PROCEDURE'!!! (otherwise it simply will not save) Anybody else have this happen? (See below, others have had this happen over the years but it's not clear what the workaround is) Keep in mind this is my first attempt and I have ordered some specialized books on SQL, but if this is a common problem (and I suspect it's some sort of bug or quirk in VS2005), please let me know. Frankly I think SQL as done by VS2005 is messed up. Here are two Usenet threads on this problem: (1) http://tinyurl.com/2o956m or, http://groups.google.com/group/micro...454182ae77d409 (2) http://tinyurl.com/2ovybv or, http://groups.google.com/group/micro...e5428bf0525889 The second thread implies this is a bug--any fix? Also this bug might be relate to the fact I've switched (and not rebooted) from Administrator to PowerUser after successfully changing the permissions in the SQL Server Management Studio Express (see this thread: http://tinyurl.com/2o5yqa ) Regarding this problem I might try again tommorrow to see if rebooting helps. BTW, in the event I can't get this to work, what other SQL editor/ compiler should I use besides MS Visual Studio 2005 for ADO.NET and SQL dB development? RL // source files // error message: 'Authors' table saved successfully 'Content' table - Unable to create relationship 'FK_Content_Authors'. The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Content_Authors". The conflict occurred in database "DCV_DB", table "dbo.Authors", column 'AuthorID'. // due to the below no doubt! -- CREATE PROCEDURE dbo.InsertAuthor /* THIS IS CORRECT (what I want) 'CREATE PROCEDURE' not 'ALTER PROCEDURE'*/ ( @LastName NVARCHAR(32) = NULL, @FirstName NVARCHAR(32) = NULL ) AS /* SET NOCOUNT ON */ INSERT INTO Authors (LastName, FirstName) VALUES (@LastName, @FirstName) RETURN -- ALTER PROCEDURE dbo.InsertAuthor /* WRONG! I want 'CREATE PROCEDURE' not 'ALTER PROCEDURE' but VS2005 won't save it as such!!!*/ ( @LastName NVARCHAR(32) = NULL, @FirstName NVARCHAR(32) = NULL ) AS /* SET NOCOUNT ON */ INSERT INTO Authors (LastName, FirstName) VALUES (@LastName, @FirstName) RETURN -- *|* Error message given: when trying to save CREATE PROCEDURE Stored Procedure: "There is already an object named 'InsertAuthor' in the dB |
#3
| |||
| |||
|
|
// error message: 'Authors' table saved successfully 'Content' table - Unable to create relationship 'FK_Content_Authors'. The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Content_Authors". The conflict occurred in database "DCV_DB", table "dbo.Authors", column 'AuthorID'. // due to the below no doubt! -- CREATE PROCEDURE dbo.InsertAuthor /* THIS IS CORRECT (what I want) 'CREATE PROCEDURE' not 'ALTER PROCEDURE'*/ |
#4
| |||
| |||
|
|
Note by the way that this is all SQL commands. While point-and-click GUIs for table design can make you work faster if you are unexperienced, learning the SQL commands is absolutely essential if you plan for a professional career in SQL programming. Once you know the commands well, you can use GUI tools if you find that more convenient. But all the tools do is to generate the commands like the above, and without knowledge and understanding of that, you are pretty clueless when things go wrong. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#5
| |||
| |||
|
|
For some strange reason I was able to "save" the same example while as Administrator rather than PowerUser, but I think it's just a coincidence |
|
Seems like SQL is like a macro language that is "top-down" procedural and definitely not object oriented of course. |
#6
| |||
| |||
|
|
-- |
#7
| |||
| |||
|
|
Thanks Erland--right now another problem that's sprung up is connecting to a simple database (the same one, this time the database 'works' fine, but accessing it from outside the SQL language via a console C++ program is the problem). See thread here: http://tinyurl.com/3e2nz8 |
#8
| |||
| |||
|
|
raylopez99 (raylope... (AT) yahoo (DOT) com) writes: See thread here: http://tinyurl.com/3e2nz8 You have a connection string that goes: connection->ConnectionString = "User ID=sa; Password=;" "Data Source=(local); Initial Catalog=DCV_DB;"; But didn't you say you were using SQL Express? By default, SQL Express is installed as a named instance with the name SQLEXPRESS, in which case the Data Source should be: (local)\SQLEXPRESS. |
|
Did the User ID and Password come from that book? |
|
If you want your app to run with Integrated Security, you should use Integrated Security=SSPI; instead. Having sa with a blank password is extremely bad practice, and outright dangerous if the server is exposed on the Internet. |
#9
| |||
| |||
|
|
Yes, but for some reason \SQLEXPRESS doesn't compile in VS2005 "S not recognized" error |
#10
| |||
| |||
|
|
raylopez99 (raylope... (AT) yahoo (DOT) com) writes: Yes, but for some reason \SQLEXPRESS doesn't compile in VS2005 "S not recognized" error If you program in C++ (and I assume C#) you need to double the \, as \ has a special meaning in string literals in C++. -- |
![]() |
| Thread Tools | |
| Display Modes | |
| |