Tuesday, March 27, 2012
Create table script without drop
I'm currently working on a project where we have several customers with
the same application. The database is constantly being changed and it's
hard to keep track of all the changes from all the versions in the
customers' systems.
Usually I create the changes script every time I alter any of the tables
but there is always a risk of loosing them. I wonder if there is anyway
of creating a script that updates all the tables instead of dropping and
creating them all, so our customers won't loose the database records.
Thanks in advance,
Hugo MadureiraHugo,
You can use the ALTER TABLE Statement instead of DROP TABLE & CREATE
TABLE.
eg.
Alter Table MyTable
Add MyColumn varchar(10)
HTH
Barry|||You can get rid of a lot of headaches by using SQL Compare.
www.red-gate.com
"Hugo Madureira" <hugomadureira@.hotmail.com> wrote in message
news:%232216JpJGHA.3696@.TK2MSFTNGP15.phx.gbl...
> Hello all!
> I'm currently working on a project where we have several customers with
> the same application. The database is constantly being changed and it's
> hard to keep track of all the changes from all the versions in the
> customers' systems.
> Usually I create the changes script every time I alter any of the tables
> but there is always a risk of loosing them. I wonder if there is anyway of
> creating a script that updates all the tables instead of dropping and
> creating them all, so our customers won't loose the database records.
>
> Thanks in advance,
> Hugo Madureira|||Of course, this gets more complex than just adding columns. Such as
adding/removing columns with check constraints, foreign key constraints,
primary key constraints, unique constraints, computed columns, changing
datatypes/scale/precision, etc. Not all table changes are adding columns.
"Barry" <barry.oconnor@.singers.co.im> wrote in message
news:1138731694.928227.324210@.z14g2000cwz.googlegroups.com...
> Hugo,
> You can use the ALTER TABLE Statement instead of DROP TABLE & CREATE
> TABLE.
> eg.
> Alter Table MyTable
> Add MyColumn varchar(10)
>
> HTH
> Barry
>|||Ahh now I understand what he *actually* wanted... oops!
Barry|||I was looking for a possible way of doing that with Enterprise Manager
manager, in a way that it could be done automatically.
When I use Enterprise Manager to create a table script, it drops the
table and re-creates it. That causes data loss in the database.
If there is no way of doing that, is it possible to easily edit the
script generated by Enterprise Manager to do that?
Barry wrote:
> Hugo,
> You can use the ALTER TABLE Statement instead of DROP TABLE & CREATE
> TABLE.
> eg.
> Alter Table MyTable
> Add MyColumn varchar(10)
>
> HTH
> Barry
>
create table riht on one specified table to one user
Hi
I am not at all sure why you wish to do this! If the structure does not
change then you can just truncate the table, if you do have a variable
structure it will probably lead to a nightmare of dynamic code. You should
write a stored procedure to encapsulate this process.
John
"sunil" wrote:
> how can i give rights to the a user to create and drop one specified table
>
>
|||You can do
GRANT CREATE TABLE TO username
Once the user has created the table, you can revoke above. Then user will now own the table and be
able to drop it. The user will not be able to specify some other user as owner, for that the
db_ddladmin role is needed (which is documented in Books Online what permissions comes with it).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"sunil" <sunil@.discussions.microsoft.com> wrote in message
news:BE56743E-05C8-4054-BA21-F997B63A53BD@.microsoft.com...
> how can i give rights to the a user to create and drop one specified table
>
>
|||Thanks for the reply but i don't know at wht time the user will create and
drop the table, In a store procedure he will create the table and in the end
the user drop the table so every time that store procedure runs the user
require the create right and drop table rihts but if i give him ddladmin then
he can create other table and drop other table also . So it is security risk,
hence i require that he user can create one specified table that is mention
in the stored procedure
"Tibor Karaszi" wrote:
> You can do
> GRANT CREATE TABLE TO username
> Once the user has created the table, you can revoke above. Then user will now own the table and be
> able to drop it. The user will not be able to specify some other user as owner, for that the
> db_ddladmin role is needed (which is documented in Books Online what permissions comes with it).
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "sunil" <sunil@.discussions.microsoft.com> wrote in message
> news:BE56743E-05C8-4054-BA21-F997B63A53BD@.microsoft.com...
>
|||Hi,
When u will create the table it will be created under that user and u
can use grant create table
but if u created the table under dbo user then everyone who has access
to that database will have access.
hoipe this help
from
Killer
|||Hi
Use a temporary table if it only needed in the scope of the procedure.
John
"sunil" wrote:
[vbcol=seagreen]
> Thanks for the reply but i don't know at wht time the user will create and
> drop the table, In a store procedure he will create the table and in the end
> the user drop the table so every time that store procedure runs the user
> require the create right and drop table rihts but if i give him ddladmin then
> he can create other table and drop other table also . So it is security risk,
> hence i require that he user can create one specified table that is mention
> in the stored procedure
> "Tibor Karaszi" wrote:
create table riht on one specified table to one user
I am not at all sure why you wish to do this! If the structure does not
change then you can just truncate the table, if you do have a variable
structure it will probably lead to a nightmare of dynamic code. You should
write a stored procedure to encapsulate this process.
John
"sunil" wrote:
> how can i give rights to the a user to create and drop one specified table
>
>|||You can do
GRANT CREATE TABLE TO username
Once the user has created the table, you can revoke above. Then user will no
w own the table and be
able to drop it. The user will not be able to specify some other user as own
er, for that the
db_ddladmin role is needed (which is documented in Books Online what permiss
ions comes with it).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"sunil" <sunil@.discussions.microsoft.com> wrote in message
news:BE56743E-05C8-4054-BA21-F997B63A53BD@.microsoft.com...
> how can i give rights to the a user to create and drop one specified table
>
>|||Thanks for the reply but i don't know at wht time the user will create and
drop the table, In a store procedure he will create the table and in the end
the user drop the table so every time that store procedure runs the user
require the create right and drop table rihts but if i give him ddladmin the
n
he can create other table and drop other table also . So it is security risk
,
hence i require that he user can create one specified table that is mention
in the stored procedure
"Tibor Karaszi" wrote:
> You can do
> GRANT CREATE TABLE TO username
> Once the user has created the table, you can revoke above. Then user will
now own the table and be
> able to drop it. The user will not be able to specify some other user as o
wner, for that the
> db_ddladmin role is needed (which is documented in Books Online what permi
ssions comes with it).
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "sunil" <sunil@.discussions.microsoft.com> wrote in message
> news:BE56743E-05C8-4054-BA21-F997B63A53BD@.microsoft.com...
>|||Hi,
When u will create the table it will be created under that user and u
can use grant create table
but if u created the table under dbo user then everyone who has access
to that database will have access.
hoipe this help
from
Killer|||Hi
Use a temporary table if it only needed in the scope of the procedure.
John
"sunil" wrote:
[vbcol=seagreen]
> Thanks for the reply but i don't know at wht time the user will create and
> drop the table, In a store procedure he will create the table and in the e
nd
> the user drop the table so every time that store procedure runs the user
> require the create right and drop table rihts but if i give him ddladmin t
hen
> he can create other table and drop other table also . So it is security ri
sk,
> hence i require that he user can create one specified table that is mentio
n
> in the stored procedure
> "Tibor Karaszi" wrote:
>sql
create table riht on one specified table to one user
I am not at all sure why you wish to do this! If the structure does not
change then you can just truncate the table, if you do have a variable
structure it will probably lead to a nightmare of dynamic code. You should
write a stored procedure to encapsulate this process.
John
"sunil" wrote:
> how can i give rights to the a user to create and drop one specified table
>
>|||You can do
GRANT CREATE TABLE TO username
Once the user has created the table, you can revoke above. Then user will now own the table and be
able to drop it. The user will not be able to specify some other user as owner, for that the
db_ddladmin role is needed (which is documented in Books Online what permissions comes with it).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"sunil" <sunil@.discussions.microsoft.com> wrote in message
news:BE56743E-05C8-4054-BA21-F997B63A53BD@.microsoft.com...
> how can i give rights to the a user to create and drop one specified table
>
>|||Thanks for the reply but i don't know at wht time the user will create and
drop the table, In a store procedure he will create the table and in the end
the user drop the table so every time that store procedure runs the user
require the create right and drop table rihts but if i give him ddladmin then
he can create other table and drop other table also . So it is security risk,
hence i require that he user can create one specified table that is mention
in the stored procedure
"Tibor Karaszi" wrote:
> You can do
> GRANT CREATE TABLE TO username
> Once the user has created the table, you can revoke above. Then user will now own the table and be
> able to drop it. The user will not be able to specify some other user as owner, for that the
> db_ddladmin role is needed (which is documented in Books Online what permissions comes with it).
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "sunil" <sunil@.discussions.microsoft.com> wrote in message
> news:BE56743E-05C8-4054-BA21-F997B63A53BD@.microsoft.com...
> > how can i give rights to the a user to create and drop one specified table
> >
> >
> >
>|||Hi,
When u will create the table it will be created under that user and u
can use grant create table
but if u created the table under dbo user then everyone who has access
to that database will have access.
hoipe this help
from
Killer|||Hi
Use a temporary table if it only needed in the scope of the procedure.
John
"sunil" wrote:
> Thanks for the reply but i don't know at wht time the user will create and
> drop the table, In a store procedure he will create the table and in the end
> the user drop the table so every time that store procedure runs the user
> require the create right and drop table rihts but if i give him ddladmin then
> he can create other table and drop other table also . So it is security risk,
> hence i require that he user can create one specified table that is mention
> in the stored procedure
> "Tibor Karaszi" wrote:
> > You can do
> >
> > GRANT CREATE TABLE TO username
> >
> > Once the user has created the table, you can revoke above. Then user will now own the table and be
> > able to drop it. The user will not be able to specify some other user as owner, for that the
> > db_ddladmin role is needed (which is documented in Books Online what permissions comes with it).
> > --
> > Tibor Karaszi, SQL Server MVP
> > http://www.karaszi.com/sqlserver/default.asp
> > http://www.solidqualitylearning.com/
> > Blog: http://solidqualitylearning.com/blogs/tibor/
> >
> >
> > "sunil" <sunil@.discussions.microsoft.com> wrote in message
> > news:BE56743E-05C8-4054-BA21-F997B63A53BD@.microsoft.com...
> > > how can i give rights to the a user to create and drop one specified table
> > >
> > >
> > >
> >
> >
create table from select result
I need to create a table which has the columns from the select statement result.
I tried in this way
drop table j9a
SELECT er.* into j9a
FROM caCase c
LEFT OUTER JOIN paPatient pp ON c.caCaseID=pp.caCaseID
Left Outer JOIN paManagementSite pm ON pp.paManagementSiteID=pm.paManagementSiteID
Left Join exexposure ee ON ee.cacaseID=c.caCaseID
LEFT OUTER JOIN exExposureRoute eer ON eer.caCaseID=c.caCaseID
LEFT OUTER JOIN exRoute er ON er.exRouteID=eer.exRouteID
WHERE c.caCallTypeID =0
AND c.Startdate between '1/1/2006' and '12/1/2006'
AND (ee.exMedicalOutcomeID=4 OR ee.exMedicalOutcomeID=10)
AND pp.paSpeciesID=1
AND c.PublicID_adOrganization_secondary is null
declare @.1 int,@.2 int,@.3 int,@.4 int,@.5 int,@.6 int,@.7 int,@.8 int,
@.9 int,@.10 int,@.11 int,@.12 int,@.21 int,@.22 int,@.23 int,@.24 int,@.25 int,
@.26 int,@.27 int,@.28 int,@.29 int,@.30 int,@.31 int,@.32 int
set @.21=(select count(*) from j9a where Ingestion=1)
set @.22=(select count(*) from j9a where Inhalation=1)
set @.23=(select count(*) from j9a where Aspiration=1)
set @.24=(select count(*) from j9a where Ocular=1)
set @.25=(select count(*) from j9a where Dermal=1)
set @.26=(select count(*) from j9a where Bite=1)
set @.27=(select count(*) from j9a where Parenteral=1)
set @.28=(select count(*) from j9a where Otic=1)
set @.29=(select count(*) from j9a where Rectal=1)
set @.30=(select count(*) from j9a where Vaginal=1)
set @.31=(select count(*) from j9a where Other=1)
set @.32=(select count(*) from j9a where Unknown=1)
The exRoute result is like this
70 Ingestion
71 Inhalation/nasal
72 Aspiration (with ingestion)
73 Ocular
74 Dermal
75 Bite/sting
76 Parenteral
77 Other
78 Unknown
524 Otic
525 Rectal
526 Vaginal
The above giving the errors Msg 207, Level 16, State 1, Line 19
Invalid column name 'Route_Ingestion'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'Route_Inhalation'
How to create table j9a.j9a has the columns from select
Thanks in advance
Are the columns [Route_Ingestion] and [Route_Inhalation] in the table exRoute?
|||
Hi,
I modified the post,can you please look at that
|||With your lastest modification, do you still get the same error?
It would be helpful if you were to post the entire set of code. Your error message indicates that somewhere in the code you are requesting something to do with columns named [Route_Ingestion] and [Route_Inhalation]. Perhaps there is an error with the column names somewhere, but without being able to see the entire bit of code, it is impossible to help you.
|||Msg 207, Level 16, State 1, Line 19
Invalid column name 'Route_Ingestion'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'Route_Inhalation'
Hi,
My complete code is
drop table j9a
SELECT er.* into j9a
FROM caCase c
LEFT OUTER JOIN paPatient pp ON c.caCaseID=pp.caCaseID
Left Outer JOIN paManagementSite pm ON pp.paManagementSiteID=pm.paManagementSiteID
Left Join exexposure ee ON ee.cacaseID=c.caCaseID
LEFT OUTER JOIN exExposureRoute eer ON eer.caCaseID=c.caCaseID
LEFT OUTER JOIN exRoute er ON er.exRouteID=eer.exRouteID
WHERE c.caCallTypeID =0
AND c.Startdate between '1/1/2006' and '12/1/2006'
AND (ee.exMedicalOutcomeID=4 OR ee.exMedicalOutcomeID=10)
AND pp.paSpeciesID=1
AND c.PublicID_adOrganization_secondary is null
declare @.1 int,@.2 int,@.3 int,@.4 int,@.5 int,@.6 int,@.7 int,@.8 int,
@.9 int,@.10 int,@.11 int,@.12 int,@.21 int,@.22 int,@.23 int,@.24 int,@.25 int,
@.26 int,@.27 int,@.28 int,@.29 int,@.30 int,@.31 int,@.32 int
set @.21=(select count(*) from j9a where Ingestion=1)
set @.22=(select count(*) from j9a where Inhalation=1)
set @.23=(select count(*) from j9a where Aspiration=1)
set @.24=(select count(*) from j9a where Ocular=1)
set @.25=(select count(*) from j9a where Dermal=1)
set @.26=(select count(*) from j9a where Bite=1)
set @.27=(select count(*) from j9a where Parenteral=1)
set @.28=(select count(*) from j9a where Otic=1)
set @.29=(select count(*) from j9a where Rectal=1)
set @.30=(select count(*) from j9a where Vaginal=1)
set @.31=(select count(*) from j9a where Other=1)
set @.32=(select count(*) from j9a where Unknown=1)
Create table table9(Route varchar(30),Fatal int)
insert into table9 values ('Ingestion',@.1,@.21)
insert into table9 values ('Inhalation',@.2,@.22)
insert into table9 values ('Aspiration',@.3,@.23)
insert into table9 values ('Ocular',@.4,@.24)
insert into table9 values ('Dermal',@.5,@.25)
insert into table9 values ('Bite',@.6,@.26)
insert into table9 values ('Parenteral',@.7,@.27)
insert into table9 values ('Otic',@.8,@.28)
insert into table9 values ('Rectal',@.9,@.29)
insert into table9 values ('Vaginal',@.10,@.30)
insert into table9 values ('Other',@.11,@.31)
insert into table9 values ('Unknown',@.12,@.32)
select * from table9
and Select exrouteid,name from exRoute;
Will give results like this
70 Ingestion
71 Inhalation
72 Aspiration 73 Ocular
74 Dermal
75 Bite/sting
76 Parenteral
77 Other
78 Unknown
524 Otic
525 Rectal
526 Vaginal
But still iam getting errors Like
Invalid column name 'Ingestion'.
For all columns this error is coming
Thanks in advance
|||
There is a whole 'mess' of problems here.
First, Table j9a (which comes from exRount) contains does not appear to contain columns named 'Ingestion', 'Inhalation', etc. ( Select exrouteid,name from exRoute ) Those are values stored in the column named 'Name'. Therefore all of the variable assignments using SELECT will fail since they are requesting data from a column named 'Ingestion', etc. ( set @.21=(select count(*) from j9a where Ingestion=1) )
Then you create a table [Table9], which has two (2) columns, (Create table table9(Route varchar(30),Fatal int)) and attempt to INSERT three values into that table. ( insert into table9 values ('Ingestion',@.1,@.21))
There may be additional problems, but I stopped lookiing at this point. Apparantly, you are attempting to so something a bit more complex that your current skills. (Nothing wrong with that, we all have to learn and progress.) But this code cannot work, and is most likely NOT the best way to create a solution.
I suggest that you post the TABLE DDL, some sample data in the form of INSERT Statements, and a description of what you are attempting to accomplish and perhaps we can help you. See this link for help in preparing the information we need in order to help you. http://www.aspfaq.com/5006
|||Hi,
I found where the problem is.
I modified the stored procedure entirely.
Now iam getting the result
Thanks
Thursday, March 22, 2012
Create Store Procedure to Paging!
Hi All!
I have Store Procedure:
If exists(Select * From sysobjects Where Name like 'Forum_Topic_SelectFromForum')
Drop Procedure Forum_Topic_SelectFromForum
go
CREATE PROCEDURE Forum_Topic_SelectFromForum
(
@.ForumID varchar(10)
)
AS BEGIN TRANSACTION
SELECT * from Forum_Topic whereForumID=@.ForumID Order by Tmp DESC
IF @.@.ERROR <> 0
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION
Now, I want to Add 2 Variables: @.Offset int, @.Count int . With @.Offset: the point of data, @.Count: sum of row will get.
when get data I want it get from @.Offset to Added @.Count.
Help me to rewrite this store procedure. Thanks
Hi duynnh,
Are you using SQL 2005? That makes it really easy. Seethis blog post for an example of how you can use a CTE and the new Row_Number() function.
If you're not using 2005 you can do it by creating dynamic SQL. Check outthis post for a generic 'returnpage' stored procedure. Other options include using a temp table or possibly some trickery involving set rowcount if your schema works nicely with that method. Seehere for a more thorough examination of your options.
I hope that helps.
|||Try the code below (assuming you are using sql 2005/express). I've tested in my side, it works fine
CREATE PROCEDURE Forum_Topic_SelectFromForum( @.ForumID varchar(10), @.offsetint, @.countint)AS BEGIN TRANSACTIONselect * from(SELECT *,row_number() over( Order by Tmp DESC)as row from Forum_Topic where ForumID=@.ForumID )as testwhere test.row between @.offset and @.offset+@.countIF @.@.ERROR <> 0 ROLLBACK TRANSACTIONELSE COMMIT TRANSACTION
Saturday, February 25, 2012
create index with drop existing
when dbcc checktable is run, at times it does not finish
at all, when killed and again submitted runs okay.
Vinodh,
Are you saying that the dbcc does not finish or the create index? How long
did you wait? How big is the table, and how many indexes on there?
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
"Vinodh" <anonymous@.discussions.microsoft.com> wrote in message
news:b85901c437d6$ef3b81c0$a401280a@.phx.gbl...
> Create index with drop existing after succesfull creation,
> when dbcc checktable is run, at times it does not finish
> at all, when killed and again submitted runs okay.
>
create index with drop existing
when dbcc checktable is run, at times it does not finish
at all, when killed and again submitted runs okay.Vinodh,
Are you saying that the dbcc does not finish or the create index? How long
did you wait? How big is the table, and how many indexes on there?
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
"Vinodh" <anonymous@.discussions.microsoft.com> wrote in message
news:b85901c437d6$ef3b81c0$a401280a@.phx.gbl...
> Create index with drop existing after succesfull creation,
> when dbcc checktable is run, at times it does not finish
> at all, when killed and again submitted runs okay.
>
create index with drop existing
when dbcc checktable is run, at times it does not finish
at all, when killed and again submitted runs okay.Vinodh,
Are you saying that the dbcc does not finish or the create index? How long
did you wait? How big is the table, and how many indexes on there?
--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
"Vinodh" <anonymous@.discussions.microsoft.com> wrote in message
news:b85901c437d6$ef3b81c0$a401280a@.phx.gbl...
> Create index with drop existing after succesfull creation,
> when dbcc checktable is run, at times it does not finish
> at all, when killed and again submitted runs okay.
>
CREATE INDEX statement for SQL Mobile not documented?
I'm unable to find documentation for the CREATE INDEX statement for SQL Mobile. DROP INDEX is documented however. Is this a known issue or am I missing something? This can be seen here:
http://msdn2.microsoft.com/en-us/library/ms173440(en-US,SQL.90).aspx
If you scroll the left pane up a little, you will notice that CREATE INDEX is not in the tree. Am I missing something?
Thanks.Thanks Ravi for catching this. And we are aware of it too.
We already have a bug opened in our Bug Database for this.
The next web site refresh should pickup the fix.
Thanks,
Laxmi NRO, MSFT, SQL Mobile, Microsoft Corporation|||Yes, the topic will be available in the December download version of SQL Server Books Online.
In the meantime, you can use the CREATE INDEX topic in the full version of Books Online : http://msdn2.microsoft.com/en-us/library/ms188783(en-US,SQL.90).aspx
Regards,|||
Finally it is live in SQL Mobile Books Online @. http://msdn2.microsoft.com/en-us/library/ms345331(en-US,SQL.90).aspx
Thanks,.
Laxmi Narsimha Rao ORUGANTI, MSFT, SQL Mobile, Microsoft Corporation
CREATE INDEX statement for SQL Mobile not documented?
I'm unable to find documentation for the CREATE INDEX statement for SQL Mobile. DROP INDEX is documented however. Is this a known issue or am I missing something? This can be seen here:
http://msdn2.microsoft.com/en-us/library/ms173440(en-US,SQL.90).aspx
If you scroll the left pane up a little, you will notice that CREATE INDEX is not in the tree. Am I missing something?
Thanks.Thanks Ravi for catching this. And we are aware of it too.
We already have a bug opened in our Bug Database for this.
The next web site refresh should pickup the fix.
Thanks,
Laxmi NRO, MSFT, SQL Mobile, Microsoft Corporation|||Yes, the topic will be available in the December download version of SQL Server Books Online.
In the meantime, you can use the CREATE INDEX topic in the full version of Books Online : http://msdn2.microsoft.com/en-us/library/ms188783(en-US,SQL.90).aspx
Regards,|||
Finally it is live in SQL Mobile Books Online @. http://msdn2.microsoft.com/en-us/library/ms345331(en-US,SQL.90).aspx
Thanks,.
Laxmi Narsimha Rao ORUGANTI, MSFT, SQL Mobile, Microsoft Corporation