Showing posts with label point. Show all posts
Showing posts with label point. Show all posts

Friday, March 9, 2012

Invalid Column Error - Any ideas

Hi,

Can anybody point me why I get the error "Invalid Column Name - emp" error.

Thanks.

Table structures are:

Tree

create table tree
(emp Char(10) Not Null,
Boss Char(10));


Stack

CREATE TABLE Stack
(Stack_top INTEGER Not Null,
emp Char(10) Not Null,
Lft Integer NOT NULL UNIQUE CHECK (Lft > 0),
Rgt Integer NOT NULL UNIQUE CHECK (Rgt > 1));


I am trying to convert the Tree (Adjancency Model) to Stack(Nested Model).

CODE

BEGIN
DECLARE @.lft_rgt INTEGER, @.stack_pointer INTEGER, @.max_lft_rgt INTEGER;

SET @.max_lft_rgt = 2 * (SELECT COUNT(*) FROM Tree);

INSERT INTO Stack
SELECT 1, emp, 1, @.max_lft_rgt
FROM Tree
WHERE boss IS NULL;

SET @.lft_rgt = 2;
SET @.stack_pointer = 1;

DELETE FROM Tree
WHERE boss IS NULL;

-- The stack is ready and ready to use

WHILE (@.lft_rgt < @.max_lft_rgt)
Begin
--here is where I get the 1st Invalid column name 'emp'.

IF EXISTS(SELECT * FROM Stack AS S1, Tree AS T1
WHERE S1.emp = T1.boss
AND S1.stack_top = @.stack_pointer)
BEGIN --push when stack_top has subordinates and set the lft value
--2nd invalid column error 'emp'
INSERT INTO Stack
SELECT (@.stack_pointer + 1), MIN(T1.emp), @.lft_rgt, NULL
FROM Stack AS S1, Tree AS T1
WHERE S1.emp = T1.boss
AND S1.stack_top = @.stack_pointer;

--remove this row from Tree

DELETE FROM Tree
WHERE emp = (SELECT emp FROM Stack WHERE stack_top = @.stack_pointer + 1);

SET @.stack_pointer = @.stack_pointer + 1;
END --push
ELSE
BEGIN --pop the stack and set the rgt value
UPDATE Stack
SET rgt = @.lft_rgt,
stack_top = -stack_top
WHERE stack_top = @.stack_pointer

SET @.stack_pointer = @.stack_pointer - 1;
END --pop

END; -- IF
END; --While

Does dropping the "AS" in the table alias help? It looks okay to me, except for that...

So:

select * from Stack S1, Tree T1 where S1.emp = T1.boss........|||

I don′t think that the AS is the problem. Make sure that the tables are really created with the specified columns. Make sure that you have a look at the right owner / schema, not working on an old schema. I addition you should always specifiy the column where you want to insert the values to make the code more readable for others:

INSERT INTO TableA
(
COLA,
COLB
)
(...)

HTH, Jens K. Suessmeyer.


http://www.sqlserver2005.de

|||

Jens K. Suessmeyer wrote:

I don′t think that the AS is the problem. Make sure that the tables are really created with the specified columns. Make sure that you have a look at the right owner / schema, not working on an old schema. I addition you should always specifiy the column where you want to insert the values to make the code more readable for others:

INSERT INTO TableA
(
COLA,
COLB
)
(...)

HTH, Jens K. Suessmeyer.


http://www.sqlserver2005.de

It must be a valid table, unless we're not getting all of the error specifics. The first part of his stored proc code, he inserts into the stack table...

Would it be better to setup a cursor instead of using the "IF EXISTS" syntax?|||

I do not get any errors now. I made the changes to my insert statements by specifying the columns. I am following the example of Nested Sets as mentioned by Joe Celko's SQL Trees. His example is in this link: http://www.I am trying with the simple data structure as mentioned here. When I run it now, it is still running as I am typing this message. If I know what is wrong, then I shall know where to look for my errors. I do not have any other table by these names. I double checked in the Enterprise Manager.

This is what I had typed in the Query Analyzer and tried to run it.

-

--How can I check if the table Tree exists. Only if it exists, then I would like to drop the table Tree.

DROP TABLE Tree;
create table tree
(emp Char(10) Not Null,
Boss Char(10));

--Insert sample data into tree
INSERT INTO TREE Values ('Albert', NULL);
INSERT INTO TREE Values ('Bert', 'Albert');
INSERT INTO TREE Values ('Chuck', 'Albert');
INSERT INTO TREE Values ('Donna', 'Chuck');
INSERT INTO TREE Values ('Eddie', 'Chuck');
INSERT INTO TREE Values ('Fred', 'Chuck');

DROP Table Stack;

CREATE TABLE Stack
(Stack_top INTEGER Not Null,
emp Char(10) Not Null,
Lft Integer NOT NULL UNIQUE CHECK (Lft > 0),
Rgt Integer NOT NULL UNIQUE CHECK (Rgt > 1));

--Select * from Tree
--Select * from Stack

BEGIN
DECLARE @.lft_rgt INTEGER, @.stack_pointer INTEGER, @.max_lft_rgt INTEGER;

SET @.max_lft_rgt = 2 * (SELECT COUNT(*) FROM Tree);

INSERT INTO Stack (Stack_top, emp, Lft, Rgt)
SELECT 1, emp, 1, @.max_lft_rgt
FROM Tree
WHERE boss IS NULL;

SET @.lft_rgt = 2;
SET @.stack_pointer = 1;

DELETE FROM Tree
WHERE boss IS NULL;

-- The stack is ready and ready to use

WHILE (@.lft_rgt < @.max_lft_rgt)
Begin
IF EXISTS(SELECT * FROM Stack AS S1, Tree AS T1
WHERE S1.emp = T1.boss
AND S1.stack_top = @.stack_pointer)
BEGIN --push when stack_top has subordinates and set the lft value
INSERT INTO Stack(Stack_top, emp, Lft, Rgt)
SELECT (@.stack_pointer + 1), MIN(T1.emp), @.lft_rgt, NULL
FROM Stack AS S1, Tree AS T1
WHERE S1.emp = T1.boss
AND S1.stack_top = @.stack_pointer;

--remove this row from Tree

DELETE FROM Tree
WHERE emp = (SELECT emp FROM Stack WHERE stack_top = @.stack_pointer + 1);
SET @.stack_pointer = @.stack_pointer + 1;
END --push
ELSE
BEGIN --pop the stack and set the rgt value
UPDATE Stack
SET rgt = @.lft_rgt,
stack_top = -stack_top
WHERE stack_top = @.stack_pointer

SET @.stack_pointer = @.stack_pointer - 1;
END --pop

END; -- IF
END; --While

--

Any ideas?

Thanks. PS: I noticed that the execution took 10 minutes and 27 seconds and I had to stop it. (Isn't that too long for a table which has only 6 rows of data)

|||

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.Tables WHERE Table_name = 'Tree') --Not specifying the owner / schema, you should be sure to alway use the same owner / schema name
DROP TABLE Tree

You are missing something as you are not changing the break condition

WHILE (@.lft_rgt < @.max_lft_rgt)

Therefore it will run forever in an infinite loop.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

I am still getting an infinite loop. Where do I increment the @.lft_rgt as you mentioned that @.lft_rgt will be always less than @.max_lft_rgt.

I just copied the code of Joe Celko's SQL Trees and wanted to try it on my SQL Server before applying it to my solution. But apparently I was misled that is would be error-free :(

Any ideas what I should do to convert the table Tree to Stack?

Thanks.

Tree Table:

Emp Boss

Albert NULL
Bert Albert
Chuck Albert
Donna Chuck
Eddie Chuck
Fred Chuck

Stack Table:

Stack_top Employee Lft Rgt
1 Albert 1 12
2 Bert 2 3
3 Chuck 4 11
4 Donna 5 6
5 Eddie 7 8
6 Fred 9 10

My code now is:

[code]

IF Exists(Select * From SYSOBJECTS Where Name = 'Tree')
Begin
drop table [dbo].[Tree]
End
GO

create table tree
(emp Char(10) Not Null,
Boss Char(10))

--Insert sample data into tree
INSERT INTO TREE Values ('Albert', NULL)
INSERT INTO TREE Values ('Bert', 'Albert')
INSERT INTO TREE Values ('Chuck', 'Albert')
INSERT INTO TREE Values ('Donna', 'Chuck')
INSERT INTO TREE Values ('Eddie', 'Chuck')
INSERT INTO TREE Values ('Fred', 'Chuck')

IF Exists(Select * From SYSOBJECTS Where Name = 'Tree')
BEGIN
drop table [dbo].[Stack]
END
GO

CREATE TABLE Stack
(Stack_top INTEGER Not Null,
employee Char(10) Not Null,
Lft Integer,
Rgt Integer)

--Select * from Tree
--Select * from Stack

BEGIN

DECLARE @.lft_rgt INTEGER, @.stack_pointer INTEGER, @.max_lft_rgt INTEGER

SET @.max_lft_rgt = 2 * (SELECT COUNT(*) FROM Tree)

INSERT INTO Stack (Stack_top, employee, Lft, Rgt)
SELECT 1, emp, 1, @.max_lft_rgt
FROM Tree
WHERE boss IS NULL

SET @.lft_rgt = 2
SET @.stack_pointer = 1

DELETE FROM Tree
WHERE boss IS NULL


--Till here it is working fine. I can see the insert into Stack
--for the top most manager with Stack_top=1, emp=Albert, lft=1, rgt=12(2*6 rows of the table-tree)

--I can also see the row of Albert deleted from the table Tree because his 'Boss' column has a null value

-- The stack is ready and ready to use

--WHILE (@.lft_rgt < @.max_lft_rgt)
--Begin

SET @.max_lft_rgt = 2 * (SELECT COUNT(*) FROM Tree)
SET @.lft_rgt = 2
SET @.stack_pointer = 1

WHILE (@.lft_rgt < @.max_lft_rgt)
Begin
IF EXISTS(SELECT * FROM Stack AS S1, Tree AS T1
WHERE S1.employee = T1.boss
AND S1.stack_top = @.stack_pointer)
BEGIN --push when stack_top has subordinates and set the lft value
--DECLARE @.stack_pointer integer, @.lft_rgt integer
--SET @.stack_pointer = 1
--SET @.lft_rgt = 2
INSERT INTO Stack(Stack_top, employee, Lft, Rgt)
SELECT (@.stack_pointer + 1), MIN(T1.emp), @.lft_rgt, @.stack_pointer + 2
FROM Stack AS S1, Tree AS T1
WHERE S1.employee = T1.boss
AND S1.stack_top = @.stack_pointer;

--increment the @.lft_rgt
--SET @.lft_rgt = @.lft_rgt + 1

--remove this row from Tree
DELETE FROM Tree
WHERE emp = (SELECT employee FROM Stack WHERE stack_top = @.stack_pointer + 1);

SET @.stack_pointer = @.stack_pointer + 1;
END --push
ELSE
BEGIN --pop the stack and set the rgt value
UPDATE Stack
SET rgt = @.lft_rgt + 1,
stack_top = -stack_top
WHERE stack_top = @.stack_pointer

SET @.stack_pointer = @.stack_pointer - 1;
END --pop

END; -- IF
END; --While

|||You are not changing the condition in the loop, that why it is infinite. Increase the @.lft_rgt or decrease the @.max_lft_rgt.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de|||

Ok. I changed the code to increment the @.lft_rgt and I do not see an infinite loop.

IF Exists(Select * From Information_Schema.Tables Where Table_Name = 'Tree'
and TABLE_TYPE ='BASE TABLE')
Begin
drop table [dbo].[Tree]
End
GO

create table tree
(emp Char(10) Not Null,
Boss Char(10))

--Insert sample data into tree
INSERT INTO TREE Values ('Albert', NULL)
INSERT INTO TREE Values ('Bert', 'Albert')
INSERT INTO TREE Values ('Chuck', 'Albert')
INSERT INTO TREE Values ('Donna', 'Chuck')
INSERT INTO TREE Values ('Eddie', 'Chuck')
INSERT INTO TREE Values ('Fred', 'Chuck')

IF Exists(Select * From Information_Schema.Tables Where Table_Name = 'Stack'
and TABLE_TYPE ='BASE TABLE')
BEGIN
drop table [dbo].[Stack]
END
GO

CREATE TABLE Stack
(Stack_top INTEGER Not Null,
employee Char(10) Not Null,
Lft Integer,
Rgt Integer)

--Select * from Tree
--Select * from Stack

DECLARE @.lft_rgt INTEGER, @.max_lft_rgt INTEGER, @.stack_pointer INTEGER

Set @.lft_rgt = 2
Set @.max_lft_rgt = 2 * (Select Count(*) FROM Tree)
Set @.stack_pointer = 1

INSERT INTO Stack
SELECT 1, emp, 1, NULL
FROM Tree
Where Boss is null

Delete from Tree
Where Boss is null

While @.lft_rgt <= (@.max_lft_rgt - 2)
begin
IF EXISTS(Select * from stack as s1, tree as t1 where s1.employee=t1.boss and s1.stack_top = @.stack_pointer)
BEGIN --push when top has subordinates, set lft value
insert into stack
select(@.stack_pointer+1), min(t1.emp), @.lft_rgt, null
from stack as s1, tree as t1
where s1.employee = t1.boss
and s1.stack_top = @.stack_pointer
--delete the row from the tree table
delete from tree
where emp = (select employee from stack where stack_top = @.stack_pointer + 1)
set @.lft_rgt = @.lft_rgt + 1
set @.stack_pointer = @.stack_pointer + 1
END --push
ELSE
BEGIN -- pop the stack and set rgt value
UPDATE Stack
Set rgt = @.lft_rgt, stack_top = -stack_top --pops the stack
WHERE stack_top = @.stack_pointer
SET @.lft_rgt = @.lft_rgt + 1
Set @.stack_pointer = @.stack_pointer -1
end --pop
-- End --IF
End -- While

select * from tree

Select * from Stack

Instead of seeing my table as:

employee lft rgt
Albert 1 12
Bert 2 3
Chuck 4 11
Donna 5 6
Eddie 7 8
Fred 9 10


However, I see my result table Stack as:

Stack_top Employee Lft Rgt
1 Albert 1 NULL
-2 Bert 2 3
2 Chuck 4 NULL
-3 Donna 5 6
-3 Eddie 7 8
-3 Fred 9 10

What can I do to make 'chuck' have a value of 11 and 'Albert' have a value of 12?

Thanks.

|||

Ok, I was inserting value of Null for albert:

INSERT INTO Stack
SELECT 1, emp, 1, null
FROM Tree
Where Boss is null

I changed the query to:

INSERT INTO Stack
SELECT 1, emp, 1, @.max_lft_rgt
FROM Tree
Where Boss is null

However what can I do to change the rgt value for 'Chuck' to 11 and not null?

Any ideas?

Thanks.

Friday, February 24, 2012

Interview questions and answers

Hi,
Can someone please point me to a link which include SQL Server interview
questions and answers on database administration, performance tuning at
expert level (400)?
Thanks,
EinatTry these:
SQL Server Interview Questions
http://vyaskn.tripod.com/iq.htm
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf
"Gil" <gillapid@.google.com> wrote in message
news:%23N2%23kSw%23GHA.4376@.TK2MSFTNGP03.phx.gbl...
> Hi,
> Can someone please point me to a link which include SQL Server interview
> questions and answers on database administration, performance tuning at
> expert level (400)?
> Thanks,
> Einat
>
>

Interview questions and answers

Hi,
Can someone please point me to a link which include SQL Server interview
questions and answers on database administration, performance tuning at
expert level (400)?
Thanks,
EinatTry these:
SQL Server Interview Questions
http://vyaskn.tripod.com/iq.htm
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf
"Gil" <gillapid@.google.com> wrote in message
news:%23N2%23kSw%23GHA.4376@.TK2MSFTNGP03.phx.gbl...
> Hi,
> Can someone please point me to a link which include SQL Server interview
> questions and answers on database administration, performance tuning at
> expert level (400)?
> Thanks,
> Einat
>
>

Sunday, February 19, 2012

Interpreting SQLIO Results... What Now?

Hi all, I ran SQLIO against my SAN and got back some discouraging results.
It basically seemns to point to optimal performance settings of 64KB I/O's
and 4096 KB Stripes. My question is this: how do I ensure that Windows and
SQL Server 2K use these settings? Is there something that needs to be set
in the registry or do I need to modify settings in SQL, or what?
Thanks
P.S. - We got even better results when we set the Buffering option to "All".
How can we make sure Windows, SQL, et al. are using the same options that
SQLIO used?
Thanks
"Michael C#" <xyz@.yomomma.com> wrote in message
news:ODOA5mMKFHA.3916@.TK2MSFTNGP14.phx.gbl...
> Hi all, I ran SQLIO against my SAN and got back some discouraging results.
> It basically seemns to point to optimal performance settings of 64KB I/O's
> and 4096 KB Stripes. My question is this: how do I ensure that Windows
> and SQL Server 2K use these settings? Is there something that needs to be
> set in the registry or do I need to modify settings in SQL, or what?
> Thanks
>

Internet Merge Replication IIS

Maybe I am missing something here, but it seems like the point of doing Merge Replication via IIS is so that you do not have to expose your DB server directly to the internet (i.e. all your clients), rather you expose a webserver to the internet and the webserver handles the replication keeping your DB server closed to direct internet access and thus keeping it more secure.

However, to set up Internet Merge Replication using IIS a client has to have direct internet access to the DB server as a stored procedure needs to be run on the publisher.

So Internet Merge Replciation using IIS makes it so your DB does not have to be directly exposed to the internet except your db needs to be exposed directly to the internet so every client can execute a stored procedure against it. How does this make sense? Yeah, I can write a webservice or something that gets the SP from the client and executes it against my db server but why should I have to? Why isn't the dll that the "Configure Web Synchronization" wizard puts into place capable of doing this?

Using web sync not only "hide" your sql server from the client, but also extends the merge replication to devices as well as PCs in anywhere that has the internet access. the later is actually the main reason to have this new sync type.

Sure, one can write their own web services to do the sync ( in fact, I believe there are some companies doing that already ), but this will not fully utilize the merge replication's functionalities to the web client subscriptions as it was to the fully connected subscriptions.

hence, the "Configure web synchronization" wizard means to provide some walk throguh steps to help users to set up the IIS server for replication and not to intend to setup the webservices.

thanks

Yunwen

|||

Thank you for your reply, but your reply...parts don't make sense and other parts don't answer what I asked.

>> but also extends the merge replication to devices as well as PCs in anywhere that has the internet access. the later is actually the main reason to have this new sync type.

That is NOT the "main reason" of doing web sync via IIS. Since for a client to do merge synching offsite, they have to have internet access to the sql server location, synching could be accomplished quite nicely without IIS by merely poking the appropriate holes in your firewall to allow the needed direct sql connections to be made. Thus IIS is not what facilitates merge synching over the internet. It still seems to me that the ONLY point of doing merge replication via IIS is to hide your SQL server from direct internet access.

>>Sure, one can write their own web services to do the sync

I never said this. What I said was that to set up Merge replication a client needed direct access to a sql server because a SP (Stored Procedure) has to be run against the Publisher that registeres the subscriber with the publisher. If you are using IIS to accomplish your merge replication, then you do not have your sql server directly exposed to the internet which makes it rather hard for your subscriber to connect directly to the publisher to execute a SP. What I said was that I could write a Webservice that could facilitate the subscriber sending the SP to the publisher but that I shouldn't have to as the replisapi.dll should be doing that. I said nothing about writing a webservice to do the sync, just that as things stand now I would have to write a webservice to facilitate setting up the sync.

So again, it seems like the point of using IIS to do merge replication is to hide your SQL Server publisher from the internet. However, part of setting up a subscriber involves running a SP (Stored Procedure) against the SQL Server Publisher. If your SQL Server publisher is hidden from the internet, it makes it rather impossible for your subscriber to connect to it to run a SP against it. It seems that currently the only work around is to make your own webservice that can access the SP arguments from the subscriber and run the proper SP with the proper arguments against your publisher for you. It seems sort of silly that you would have to set that up yourself, that MS hasn't built that in to something that already exists.

The situation is analguous to the Exchange RPC over HTTPS problem that occured when Office 2003 first came out. RPC over HTTPS allows Outlook to run against an Exchange server without having to have direct port 135-139 access. However in order to set up RPC over HTTPS your computer FIRST had to be able to connect directly to Exchange on ports 135-139 (which was a problem for me since I use Comcast and Comcast blocks these ports). The only work around was to lug my computer into the office, put it on a lan with the exchange server, setup RPC over HTTPS, then take it back home.) MS has since fixed this problem. However, this seems like the exact same problem. Doing merge replication via IIS makes it so that clients do not have to connect directly to the SQL Server, however to initially set up each client, that client has to connect directly to the SQL Server. Is there a way around this?

Thank you

|||

Many people may not have ability to connect to publisher directly through port 1433. That is one of the main motivation to support https (port 443) replication. IIS server is needed to understand the https protocol.

|||

Yes, that is right along the lines of what I have been saying.

Why is this so hard to understand? I will try to make it simple.

I have a sql server that is acting as a publisher. This publisher is not directly accessable via the internet. I have set up Merge replication using IIS and https on a server running IIS. The IIS server is accessable to the internet on ports 80 and 443. Ok? Simple enough.

I want to connect a subscriber to this publisher. The subscriber is somewhere on the internet. In order for the subscriber to successfully set up its subscription, it needs to execute the Stored Procedure named "sp_addmergesubscription" ON THE PUBLISHER!

As you so aptly pointed out "Many people may not have ability to connect to publisher directly through port 1433" that being the case, how then is a subscriber supposed to run the stored procedure "sp_addmergesubscription" on the publisher?

|||

You have two choices:

1. Add the subscription at the publisher before subscriber connects. Meaning an administrator at the publisher will need to add this information, not the subscriber since the subscriber does not have access.

2. Connect as an anonymous subscriber. Meaning if subscriber cannot have their subscription added to the publication, then connect as an anonymous subscriber, which will add the info for you on your first connect.