In a t-sql 2012 stored procedure, I would like to know if my solution to solve the problem is correct and/or if you have any other suggestion(s) on how I can complete the task I am listing below:
In an existing SQL Server 2012 stored procedure, there is a table called Atrn that is truncated every night. The table Atrn has a column called ABS that is populated with incorrect data.
The goal is to place the correct value into the ABS column that is located in when the stored procedure is executing.
Note: The goal is to fix the problem now since it is a production problem. The entire stored procedure that updates the dbo.Atrn table will be rewritten in the near future.
My plan is to:
create a temp table called #Atrnwork that will contain the columns
Atrnworkid int and ABSvalue double .
The value in the column called Atrnworkid in the #Atrnwork table will obtain its value from the key of the Atrn table, called atrnid by doing a select into. At the same time, the value for ABSvalue will be obtained by running some sql when the select into occurs
The main table called Atrn will be changed with a update statement that looks something like:
Update Atrn
set ABS = ABSvalue
join Atrn.atrnid = #Atrnwork.Atrnworkid
In all can you tell me what a good solution is to solve this problem and/or display some sql on how to solve the problem listed above?
Related
so I found this sql query in a project I am succeeding. This is the first time I encountering this clause/statement. I understand that this is to look if the table exist before creating one and that Object_ID is the table name that is to be created.
My questions are:
Does sysobject mean the database?
What is the Object property?
I know that it is not the columns inside the table to be created.
The columns are : dtb_color_id and description.
can someone explain this to me. please?
IF NOT EXISTS(SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('DTB_COLOR') AND OBJECTPROPERTY(ID,'ISUserTable') = 1)
BEGIN
.......some query I understand
END
sysobjects, OBJECTPROPERTY and OBJECT_ID are used in Microsoft SQL Server. They are part of the SQL Server DMVs and system functions/procedures used to query and manipulate the metadata.
sys.sysobjects is simply the list of all objects (tables, views, SPs, functions, etc) on the server in the active database. Please note, that sys.sysobjects is deprecated and is only available for backward compatibility. Use sys.objects instead
https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/system-dynamic-management-views?view=sql-server-ver16
It has (as far as I know) no meaning in MySQL, unless somebody specifically created them.
You can also use INFORMATION_SCHEMA which is available in MySQL too (however slightly different in different RDBMS).
MSSQL INFORMATION_SCHEMA: https://learn.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/system-information-schema-views-transact-sql?view=sql-server-ver16
MySQL INFORMATION_SCHEMA: https://dev.mysql.com/doc/refman/8.0/en/information-schema.html
SQL Server has no CREATE TABLE IF NOT EXISTS construct, a variation of the mentioned condition is commonly used to imitate that.
This is a way in SQL Server to check if a table exists in the active database and to perform actions according to the result, like creating the table.
OBJECTPROPERTY simply checks (in this case) if the table is a user created one.
https://learn.microsoft.com/en-us/sql/t-sql/functions/objectproperty-transact-sql?view=sql-server-ver16
I would remove the OBJECTPROPERTY condition in case the part you understand is a CREATE TABLE statement. You don't want to create a table which has a similar name to any system table/view, also you don't want to execute the CREATE TABLE if there is a VIEW with the same name (table creation will fail)
Yes sysobject means database.
The OBJECTPROPERTY() function returns information about schema-
scoped objects in the current database. Use this to check if an
object is a table, view, stored procedure, etc. You can also use
it to check if a table has a primary key, foreign key, foreign
key reference, etc.
For more details : https://learn.microsoft.com/en-us/sql/t-sql/functions/objectpropertyex-transact-sql?view=sql-server-ver16
In this scenario it is used to check whether it is user table or
not. The result of the ISUserTable property is 1 when it is user
table otherwise returns 0.
Here the following steps are followed:
First, it executes the select statement inside the IF Exists
If the select statement returns a value that condition is TRUE for IF Exists
It starts the code inside a begin statement
DTB_COLOR - May be a stored procedure
I have been getting the -668 error trying to expand a column from 8 octets to 20 octets. I got the -668 error which when I looked it up at ibm.com I got
"-668 THE COLUMN CANNOT BE ADDED TO THE TABLE BECAUSE THE TABLE HAS AN EDIT PROCEDURE DEFINED WITH ROW ATTRIBUTE SENSITIVITY"
So reading about an edit procedure, I see:
An edit procedure receives the entire row of a base table in internal Db2 format. It can transform the row when it is stored by an INSERT or UPDATE SQL statement or by the LOAD utility. An edit procedure can be defined as WITH ROW ATTRIBUTES or WITHOUT ROW ATTRIBUTES in a CREATE TABLE statement.
I did the reorg statement mentioned at
Failing update table in db2 with SQLCODE: -668, SQLSTATE: 57016, SQLERRMC: 7;
and then I could alter the table as I wished. However, the REORG seems to be more for defragging or somehow optimizing the table, but it does not say anything about removing an edit procedure. Could someone explain how there's an edit procedure, and further it has row sensitivity, yet there's no triggers, procedures in the schema that I can see. When I generated the DDL for the table, I didn't see anything suggesting an edit procedure.
I want to check which stored procedure/function inserts/deletes/updates data to particular table with time stamp in SQL server.
I can get list of stored procedure(SP) and function that has table name in its body. but those Sp's or functions may or may not be calling the particular table
Please suggest.
I want to check which stored procedure/function inserts/deletes/updates data to particular table with time stamp in SQL server.
There is way to find tables which are modified ,but you cant get who did it
So below are the only ways
1.Enable Audit /extended events
2.Modify proc to insert data into some table with some checks like
tablename procname modifieddate
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;
On a SQL Server 2008 R2 instance, we have a table where we would like to replace an existing column with a persisted computed column. So far, so good. We'd also like to allow existing queries that attempt to update this field to be "redirected" to actually update the "real" columns that are used to generate the value of the persisted computed column.
We've tried an INSTEAD OF UPDATE trigger on the table, but get the error: "...cannot be modified because it is either a computed column or is the result of a UNION operator."
I know that you can intercept the update of a computed column that is a part of a view with an INSTEAD OF UPDATE trigger, but apparently this technique does not work directly on a table itself.
We know we can modify the query or create a view and point the query at the view to make this work, but is there any way to accomplish the update without making these changes?