Showing posts with label writing. Show all posts
Showing posts with label writing. Show all posts

Thursday, March 29, 2012

alternate to "NOT IN " query

any better way of writing this
Select serialnumber from tbl_test2
where serialnumber
not in (select serialnumber from tbl_test) and
exportedflag=0Vikram
Select serialnumber from tbl_test2
where not exists
(select * from tbl_test where tbl_test.serialnumber
=tbl_test2.serialnumber )
and exportedflag=0
"Vikram" <aa@.aa> wrote in message
news:OE88tbrBGHA.3396@.tk2msftngp13.phx.gbl...
> any better way of writing this
> Select serialnumber from tbl_test2
> where serialnumber
> not in (select serialnumber from tbl_test) and
> exportedflag=0
>|||Also:
Select SerialNumber from tbl_test2
left join tbl_test on tbl_test2.SerialNumber = tbl_test.SerialNumber
where tbl_test.serialNumber is null and exportedFlag=0
"Vikram" <aa@.aa> wrote in message
news:OE88tbrBGHA.3396@.tk2msftngp13.phx.gbl...
> any better way of writing this
> Select serialnumber from tbl_test2
> where serialnumber
> not in (select serialnumber from tbl_test) and
> exportedflag=0
>

Tuesday, March 27, 2012

Altering objects

This is a question regarding a general problem of scripting DB changes\alteration:

I'm writing script which contain few (lets say 2) DDL statements, which depend one on the other.

Each statement has it own "Go" command afterwards, for executing it.

I'm writing a script to alter a field and make it not null:

"Alter <table name> alter column <column name> int not null"

, so I can define it afterwards as a primary key, and the next statement add a primary key constraint on this filed:

Alter table <table name>

ADD CONSTRAINT <name of constraint> PRIMARY KEY (name of field).

It all goes fine if I run the statements separatelly !!, but when I try to run them in a script, there's an error message saying that a nullable field cannot be defined as primary key (I don't have the exact syntax here, I'm on a few days vacation).

It seems like the sql server dosen't recognize "quick enough" the first alter of the field, to become not null, so it can allow the field to be primary key.

Is it a problem of unupdated system tables ?

It seems like this is a general problems, because it happens to me also ina nother case, when I try to disable a table trigger, so I can delete one of it's records (the trigger dosen't allow deleting records).

Please support.

Guy

Is it a case of a missing [ GO ] between the ALTER statements, or missing parentheses around the column name?

This seems to work without error.

Code Snippet


CREATE TABLE MyTable
( RowID int IDENTITY,
MyData varchar(20),
MyCol2 char(1)
)
GO


ALTER TABLE MyTable
ALTER COLUMN MyCol2 int NOT NULL
GO


ALTER TABLE MyTable
ADD CONSTRAINT PK_MyCol2 PRIMARY KEY ( MyCol2 )
GO

|||

Guy, this is interesting - particularly as you mention that you are including GO between each statement.

Could you post your complete script to the forum for review?

Thanks

Chris

|||

Arnie,

Thanks for your reply.

The code you published is almost the same I have, except for the create table which dosen't exists in my code (in my case the table is laready exists), and also - in my script, there are few more statements. Still - dosen't work.

I'm actually looking for maybe a DBCC\system command, which can update the status of the relevant sys tables, maybe something like the DBCC UPDATEUSAGE is doing to the info of the sp_spaceused, to make the sysindexes synch with the actual data in the table. Any other solution will be helpfull.

|||

Chris,

Thank you for your reply.

I don't have the exact code here (I'm on a few days vacation), but what I generally do is (pseudo code)

Start transaction

statement 1

Go

error handeling (if @.@.error<>0 goto errorhandle

GO)

statment 2

Go

error handeling (if @.@.error<>0 goto errorhandle

GO)

errorhandle:

rollback transction

commit transaction

As I mentioned, the code run with no problems if I run it step by step, and if I fix the is null statement separatelly (run it before, seperatelly, to make it not null), then the all script run with no problems.

|||

Since the table exists, it would be helpful to post both the table DDL as well as the actual procedure code.

No, there is not a DBCC command that would do what you are asking.

The problem apparantly exists in the information we can't see.

|||

The problem appears to be due to the following:

The GO immediately after each of your IF @.@.ERROR checks. You're trying to jump to a label (errorhandle) which is declared outside of the current batch. Labels can only be referenced within the batch in which they are declared.

Sunday, March 25, 2012

alter text column

Hi

I had a text type not null column which i wanted to change to a null column.Writing a simple alter statement gave me an eror cannot change text type column so i tried to rename the original column create a new column with the same name and allowing nulls on it and then copying the contents of the renamed column to the new column and finally deleting the renamed column.

EXEC sp_rename 'TableName.ColumnName', 'ColumnName_old', 'COLUMN'

ALTER TABLE TableName ADD ColumnName text NULL
UPDATE TableName SET ColumnName = ColumnName_old
ALTER TABLE TableName DROP COLUMN ColumnName_old

However when i tried to execute these statements in query analyser on the Update statement it gave me the error that ColumnName_old does not exist.

However then I tried to execute these queries one by one I was able to do that.

Can anybody tell me whats causing the queries to not be executed all at once without giving the ColumnName_old does not exist error cause I wanted to run them on live dbs.

any help would be appreciated.

Himani

Your logic should work with a little change. Add a GO between each batch.

Like:

EXEC sp_rename 'TableName.ColumnName', 'ColumnName_old', 'COLUMN'

GO

ALTER TABLE TableName ADD ColumnName text NULL

GO
UPDATE TableName SET ColumnName = ColumnName_old

GO
ALTER TABLE TableName DROP COLUMN ColumnName_old

GO

After each DML statement executed, you should get what you want.

By the way, it seems you can change the colum with text datatype from not null to allow null directly from the table definition in SQL 2005 Management Studio.

Also, you can directly do the update like this:

UPDATE yourTable

Set yourNewcolumTextAllowNull=youroldcolumnnotAllowNull

HTH

|||

Thanks a lot limno,it worked !!!

:-)

sql

Wednesday, March 7, 2012

ALTER DATABASE dynamic SQL

I am in a bind.
I am writing a stored proc that needs to change the logical name of a
database as one step in the process. The issue is that I can't seem to pass
the database in a variable to the Alter Database command.
What are the options I have for making this work?
Code snippet:
DECLARE @.NewDB
@.NewDB = 'TestDB'
@.NewLogicalName = 'TestDB'
@.OldLogicalName = 'TemplateDB'
ALTER DATABASE @.NewDB <-- This generates an error
MODIFY FILE (
NAME = @.OldLogicalName,
NEWNAME = @.NewLogicalName
)
Thanks!!!!!
RSHRSH wrote:
> I am in a bind.
> I am writing a stored proc that needs to change the logical name of a
> database as one step in the process. The issue is that I can't seem to pa
ss
> the database in a variable to the Alter Database command.
> What are the options I have for making this work?
> Code snippet:
> DECLARE @.NewDB
> @.NewDB = 'TestDB'
> @.NewLogicalName = 'TestDB'
> @.OldLogicalName = 'TemplateDB'
> ALTER DATABASE @.NewDB <-- This generates an error
> MODIFY FILE (
> NAME = @.OldLogicalName,
> NEWNAME = @.NewLogicalName
> )
> Thanks!!!!!
> RSH
You will have to use EXEC for that. For example:
EXEC (@.alter_stmt)
Where @.alter_stmt is a string containing your ALTER DATABASE statement.
But why would you want to do such a thing in a proc?
David Portas
SQL Server MVP
--|||Thanks.
It is a long story but basically we have template databases and when a new
company is created we backup the template database and restore it to a new
name. It works good and everything changes except the Logical name so i
needed a way to change the logical name to the new database name.
This stems from the fact that our application was built using Access
databases in which this was standard procedure. We are moving over to SQL
and unfortunately we can't justify writing a few thousand lines of
application code, so we are stuck bending a few best practices here and
there to initially get things running.
Thanks,
RSH
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1133963892.610144.63210@.z14g2000cwz.googlegroups.com...
> RSH wrote:
>
> You will have to use EXEC for that. For example:
> EXEC (@.alter_stmt)
> Where @.alter_stmt is a string containing your ALTER DATABASE statement.
> But why would you want to do such a thing in a proc?
> --
> David Portas
> SQL Server MVP
> --
>|||RSH wrote:
> This stems from the fact that our application was built using Access
> databases in which this was standard procedure. We are moving over to SQL
> and unfortunately we can't justify writing a few thousand lines of
> application code
Experience suggests this is a recipe for failure. Virtually every
Access database I've ever seen is incompatible with good design in SQL
Server (or in most other database systems for that matter!). Wait until
you can justify rewriting it is my advice. No solution at all is
frequently better than a bad solution.
Good luck.
David Portas
SQL Server MVP
--|||> It is a long story but basically we have template databases and when a new
> company is created we backup the template database and restore it to a new
> name.
I use model for this. Life is much simpler (except when I need to create a
database that's not based on the template).
A|||RSH (way_beyond_oops@.yahoo.com) writes:
> It is a long story but basically we have template databases and when a new
> company is created we backup the template database and restore it to a new
> name. It works good and everything changes except the Logical name so i
> needed a way to change the logical name to the new database name.
But why would you worry about the logical name? If all files in all
databases have the same logical name, so what? Easier to manage, because
you always know what the names are. :-)
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||If the database named SALES_GA_2005 had a logical file name of SALES_FL and
physical name of SALES_2001.MDF, it could be confusing for the administrator
who manages the server.
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns9725AFA87588BYazorman@.127.0.0.1...
> RSH (way_beyond_oops@.yahoo.com) writes:
> But why would you worry about the logical name? If all files in all
> databases have the same logical name, so what? Easier to manage, because
> you always know what the names are. :-)
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||I fixed it by calling the stored procedure Alter Database from my
application rather than parameterize it in a SP.
I agree in a perfect world it would be nice to re layout the DBs from
scratch and modify the application logic to utilize it...but the fact of the
matter is the people with the wallets want a solution and while it isn't
perfect it will work for the meantime. We are going to be porting our
application to .Net next year and that is when we are going to be revamping
things to take advantage of SQL server but until then we have to keep
generating revenue.
The logical name needs to be changed because I will still be referencing the
template database by name and I can't very well have 100 logical names all
the same...that would be quite an administrative nightmare.
Thanks for the help and insight.
:-)
"JT" <someone@.microsoft.com> wrote in message
news:eB$Odu0%23FHA.3464@.TK2MSFTNGP15.phx.gbl...
> If the database named SALES_GA_2005 had a logical file name of SALES_FL
> and physical name of SALES_2001.MDF, it could be confusing for the
> administrator who manages the server.
> "Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
> news:Xns9725AFA87588BYazorman@.127.0.0.1...
>|||> If the database named SALES_GA_2005 had a logical file name of SALES_FL
> and physical name of SALES_2001.MDF, it could be confusing for the
> administrator who manages the server.
Well, the intent was probably more like giving the logical file names a more
generic descriptor, like "data" and "log"... of course the MDF/LDF file
names would have to be unique, unless each pair is in its own folder.|||RSH wrote:
> The logical name needs to be changed because I will still be referencing t
he
> template database by name and I can't very well have 100 logical names all
> the same...that would be quite an administrative nightmare.
>
Are you sure you understand what the logical file name is? A database
is never referenced by its logical file name (for one reason because
there are at least two logical file names per database). The only time
you reference the logical name is if you need to change one of the file
properties such as size, location, growth, etc. In that case you always
need the database name as well. So I can't make sense of why it would
be an "administrative nightmare" to have the same names in each DB. In
some ways it makes things easier if you do this stuff dynamically.
I do agree that it makes sense to have a good naming convention but I'm
not convinced you understand what the logical file name is for.
David Portas
SQL Server MVP
--

Sunday, February 19, 2012

Allowing "empty" value in comboBox

I'm writing a query (that will be used for a comboBox in sql reporting
services) but the user should also be able to chosse an "empty" value in
case he/she wants to leave it blank. I was thinking of using a "Union" and
attach the empty value to my recordset, but there has to be a way in
client-side for this.
I tried "Allow blank value", but it doesn't work (I don't see a blank value
anywhere)
Also, is it possible to change the combobox so it's not read-only?
Thanks.You might try "Allow Null Value"
Mine works wirth Allow Blank Value.
"VMI" <VMI@.discussions.microsoft.com> wrote in message
news:92DD1EFB-FDD0-41FF-B559-EBC93DFEB8BA@.microsoft.com...
> I'm writing a query (that will be used for a comboBox in sql reporting
> services) but the user should also be able to chosse an "empty" value in
> case he/she wants to leave it blank. I was thinking of using a "Union"
> and
> attach the empty value to my recordset, but there has to be a way in
> client-side for this.
> I tried "Allow blank value", but it doesn't work (I don't see a blank
> value
> anywhere)
> Also, is it possible to change the combobox so it's not read-only?
> Thanks.

Monday, February 13, 2012

All-of-a-sudden, I get "Invalid object name"

I have been developing a database in SQL Server Management Studio Express for the past few weeks. I have been writing stored procedures and functions, creating and deleting tables, etc., etc. and everything has been working just fine.
Today, however, if I try to run any of the functions I have written I get "Invalid object name 'functionName'."
This occurs even if I create a new function, execute the CREATE statement. Then if I refresh the Functions folder I can see my newly created function, but I cannot run it.
This is terribly frustrating.

Does anyone have any ideas as to what might have changed? I am logging in as the same user, on the same machine that I have always logged in on. I created the database that is giving the error.

When I right-click SQLEXPRESS in the object browser, and go to Permissions in the Server Properties dialog, no permissions are selected as "granted" for my login name under any of the Logins/Roles. (ex. BUILTIN\Administrators: my login name is listed in the "Explicit permissions for BUILTIN\Administrators" list, but no permissions are granted. If I go down the list selecting [checking] the various permissions, then close the dialog. When I open the dialog again, rows have been added to the list that have the Permission name, then "sa" listed as the Grantor, with that row checked. But, there are still no checks next to my login name.)

The only thing that has changed on my machine, that I am aware of, is that a Windows Authentication update ran on my machine earlier today. I am using Windows Authentication in my SQL Server instance. I assume it has something to do with that.

Please help. This is urgent. We have a presentation on this project in 3 days.
ugh.

Thanks in advance for any help you can provide.

hi,

I'd check that your database users' (database principal) default schema is still the one you are used to have too...

and obviously I'd verify all your code includes full object references in the form "schema_name.object_name" and not only

CREATE PROCEDURE usp_ProcName

....

EXEC usp_ProceureName

but

CREATE PROCEDURE the_schema.usp_ProcedureName

....

EXEC the_schema.usp_ProcedureName

and the like..

regards