SSMS: How to sort objects by date? - sql-server-2000

i created a stored procedure recently, and i would like to sort the list of stored procedures by date.
12 years ago this was easy:
What is the preferred way to accomplish this in SQL Server Management Studio?
Bonus: How to sort by owner?

Answer: It's not possible.
Upate: Possible
Sometime in the intervening 9 years since i first asked this question, SSMS added the feature that was present c. 2000: Object Explorer Details:
And you get the functionality of a user interface that was created 25 years ago:
I understand why didn't want to let see items in a folder: it's so much work. It's easier to make the user's suffer everyday, rather than the developers once.

You'll want to right-click the database that you are interested in and select the New Query button in the toolbar. In the Query window, insert the following TSQL:
SELECT type, type_desc, name, create_date, modify_date
FROM sys.objects
GO
This gives you system objects as well so you'll have to filter it accordingly. You could always change the select statement to "SELECT *" to get a quick view of the other columns available.
I wanted to give you the starting point and you can go where you want with it.

i am not sure if it's preferred or not but, i guess you can insert the result table into a temptable and write a select query for that table with the order by.
downside, you have to write the column names and types every time.
declare #temptable table(Name nvarchar(50),Owner nvarchar(50),Type nvarchar(50),CreateDate DateTime)
insert into #temptable your_procedure
select * from #temptable order by CreateDate desc

In SSMS, you can go to "Object Explorer Details" (F7 in 2008, menu in 2005/2008)
You have a choice of details view, you can see the create date.
MSDN
I suspect it isn't what you want though... and I don't know it it works with SQL Server 2000

Related

SQL - Table not found after backup

I saved a SQL table before deleting some information from it with the sql statment:
select * into x_table from y_table
After doing some operations, I want to get back some information from the table I saved with the query above. Unfortunately, MS SQL Server MGMTS shows an error saying that the table does not exist.
However, when I put the drop statement, the table is recognized - and the table is not underlined.
Any idea why this table is recognized by the drop table statement and not the select from statement. This seems strange for me.
EDIT:
Thank you
It may be that the table isn't underlined in your drop table command because its name is still in your IntelliSense cache. Select Edit -> IntelliSense -> Refresh Local Cache in SSMS (or just press Ctrl+Shift+R) and see if the table name is underlined then.
Edit:
Another possibility is that your drop table command might be in the same batch as another statement that creates the table, in which case SSMS won't underline it because it knows that even though the table doesn't exist now, it will exist by the time that command is executed. For instance:
None of the tables one, two, or three existed in my database when I took this screenshot. If I highlight line 6 and try to run it by itself, it will fail. Yet you can see that two is not underlined on line 6 because SSMS can see that if I run the whole script, the table will be created on line 5. On the other hand, three is underlined on line 9 because I commented out the code that would have created it on line 8.
All of that said, I think we might be making too much of this problem. If you try to select from a table and SQL Server tells you it doesn't exist, then it doesn't exist. You can't rely on IntelliSense to tell you that it does; the two examples above are probably not the only ways that IntelliSense might mislead you about the current status of a table.
If you want the simplest way to know whether an object with a given name (like x_table) exists, just use:
select object_id('x_table');
If this query returns null, x_table doesn't exist, regardless of what IntelliSense is telling you. If it returns non-null, then there is some object out there with that name, and then the real question is why your select statement is failing. And to answer that, I'd need to see the statement.
A lot of posts like this, you have to copy in 2 statements :
CREATE TABLE newtable LIKE oldtable;
INSERT newtable SELECT * FROM oldtable;

Is there any way to safely run SELECT INTO?

I have a script that runs a SELECT INTO into a table. To my knowledge, there are no other procedures that might be concurrently referencing/modifying this table. Once in awhile, however, I get the following error:
Schema changed after the target table was created. Rerun the Select
Into query.
What can cause this error and how do I avoid it?
I did some googling, and this link suggests that SELECT INTO cannot be used safely without some crazy try-catch-retry logic. Is this really the case?
I'm using SQLServer 2012.
Unless you really don't know the fields and data types in advance, I'd recommend first creating the table, then adding the data with an Insert statement. In your link, David Moutray suggests the same thing, here's his example code verbatim:
CREATE TABLE #TempTableY (ParticipantID INT NOT NULL);
INSERT #TempTableY (ParticipantID)
SELECT ParticipantID
FROM TableX;

SQL Developer Shortcuts

I am using SQL Developer to connect to an Oracle DB.
I would like to be able to see the constraints of a table via the command window. Usually I have to navigate the tables tree and then open the table details. Is there a shortcut to allow me to access the constraints (in particular the FK constraints) by writing a command? Ideally I want something like
desc table_name
where desc describes the table. I know desc is SQL based but are there any commands I can use within SQL developer?
Thanks in advance.
If you can browse for the table in table browser under your connection. Open it. You can find a constraints tab where you can find the information about constraints on the table. Instead you can click on table name in command window (the worksheet) press the shortcut Shift +F4 which will give the same information.
A similar question was asked here:
http://p2p.wrox.com/oracle/30730-sp_help-equivalent-oracle.html
Someone suggested:
select * from user_constraints where table_name=[yourtable]
There are other suggestions too...
The following select command will show the table details in Oracle
Select * from user_tables
where table_name='COUNTRIES'
Another solution to see the description of table in Oracle, on sql> prompt
SQL>Description Countries
or
SQL>Desc Countries
This will show the column name and datatype as well as constraints.

SQL SERVER table alias in stored proc

Is there a way to give an alias to a table which will then be referenced within a store procedure.
Declare #target= (sometalbewithveryverylonganduglyname)
Declare #source= (anothertablewithaveryuglyverylongverybadname)
Select * from #target
Insert into #target select from #source
delete from #source
Reason being, the table source and target could change (if the tablename is changed then we just change in one location), and for better readability.
In T-SQL you can't do:
Select * from #target
unless #target is defined as a table-variable, which requires you to set up the table as a user-defined type first. If you're attempting to assign the name of a physical table to the variable and access it this way you'd need to execute the statement using dynamic SQL.
exec ('Select * from #target')
If you just want to alias the table you could write your query like this:
Select * from sometalbewithveryverylonganduglyname AS t
From your question, sounds like the table name can change so you're going to need to use dynamic SQL in the end.
You can't do what you describe unless you resort to dynamic SQL as Yuck suggested, but doing that for the sake of not writing a long table name is not a valid justification. SSMS already includes intellisense and for SQL Server 2005 and below, you can use one of many of the free tools out there. That should reduce the amount of typing that you need to do as well as misspelling mistakes.
Besides, you can always do select t.column1 from really_long_and_ugly_name t ...
option 1) In that case you don't want to use a stored procedure, use some ORM library, it will allow you to switch between different table names with the same structure very easy
option 2) use views, that's what they are for, to hide underlying tables and allow those types of changes

SQL: How to copy a row in the same table updating one of the fields WITHOUT listing all fields

Hello
I searched for an answer to this question but didn't find any here.
I'm using Access 2010.
Basically, I've got a table with reports, and reports have a revision number.
I found an answer about how to copy fields and update only one of them, but it looks somewhat like this:
INSERT INTO reports (fieldA, fieldB, fieldC, revision, fieldD, fieldE)
SELECT fieldA, fieldB, fieldC, 2, fieldD, fieldE
FROM reports
WHERE <somecondition to select which report to copy>
Thing is I have a load of fields, so I'd like something that would look more like this:
INSERT INTO reports
SELECT *, revision=2
FROM reports
WHERE <somecondition to select which report to copy>
I know that code is incorrect; it's just to describe what I would like. As in, a way to not have a huge SQL line listing all the fields, but only the one I want to change.
(I want to keep a copy of previous revisions in the same table)
Thanks in advance to whoever can help :)
I'm pretty sure you can't do this in MS Access or indeed any other flavor of SQL. What you can do, as you probably already know, is use a stored procedure (called a Stored Query in Access) which takes your revision number and the id of the report to copy (or some other WHERE conditions) as arguments. This way, you still have to specify all the fields, but you do it only once and in your database instead of in your code. An example is here:
http://www.stardeveloper.com/articles/display.html?article=2001050101&page=1
HTH!
I found an interesting alternative in this question, using a temporary table
SQL clone record with a unique index
DROP TABLE #tmp_MyTable
SELECT * INTO #tmp_MyTable
FROM MyTable
WHERE MyIndentID = 165
ALTER TABLE #tmp_MyTable
DROP Column MyIndentID
INSERT INTO MyTable
SELECT *
FROM #tmp_MyTable
I can do the same, dropping the primary key and updating the revision, then copying it back to my table.
Not the solution I'm looking for, but an alternative way in the meantime.
EDIT:
Tried this solution without success:
VBA tells me something along the lines of "fields with multiple values are not allowed with SELECT INTO instructions (runtime error 3838)"
I have both an OLE field and an attachment field, which I suspect to be the cause of the error. But I need to keep them... :/