I have been trying to do something in SQL and I cannot quite find the right answer on Stack Overflow. I have a table (called RMBS_STANDARDIZED) already created and it has a column LOAN_PERC_ISSUER. The table is the union of several other tables where I standardized naming convention.
What I want to do after loading the table is to update it with this inner query (noting when I run this it returns the correct values are returned but obviously as a single column). Therefore, I want to store the returned query in the table.
SELECT
ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM
RMBS_STANDARDIZED
GROUP BY
ISSUER_SERIES
The insert query I am trying to use is
INSERT INTO RMBS_STANDARDIZED (LOAN_PERC_ISSUER)
SELECT
COALESCE(T1.LOAN_BALANCE, 0) / T2.TOTAL_BALANCE
FROM
RMBS_STANDARDIZED T1
LEFT JOIN
(SELECT
ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM
RMBS_STANDARDIZED
GROUP BY
ISSUER_SERIES) T2 ON T1.ISSUER_SERIES = T2.ISSUER_SERIES
But I get the following error
Cannot insert the value NULL into column 'ISSUER'
If I adjust the ISSUER column to allow NULLs, then the column does not update or remains NULL.
Alternatively, I have tried the following
ALTER TABLE RMBS_STANDARDIZED
ADD New_Column AS (SELECT
COALESCE(T1.LOAN_BALANCE, 0) / T2.TOTAL_BALANCE
FROM
RMBS_STANDARDIZED T1
LEFT JOIN
(SELECT
ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM
RMBS_STANDARDIZED GROUP BY ISSUER_SERIES) T2
ON T1.ISSUER_SERIES = T2.ISSUER_SERIES)
But get the following error
Subqueries are not allowed in this context. Only scalar expressions are allowed.
Any ideas would be helpful. I am better off using a CTE or creating a temp/hash table before creating the final table? I am using SQL Server and SQL Server Management Studio.
Your first query fail because LOAN_PERC_ISSUER has a NOT NULL constraint. Rewrite your query as :
SELECT COALESCE(T1.LOAN_BALANCE / T2.TOTAL_BALANCE, 0)
FROM RMBS_STANDARDIZED AS T1
LEFT OUTER JOIN (SELECT ISSUER_SERIES,
SUM(COALESCE(LOAN_BALANCE, 0)) AS TOTAL_BALANCE
FROM RMBS_STANDARDIZED
GROUP BY ISSUER_SERIES) AS T2
ON T1.ISSUER_SERIES = T2.ISSUER_SERIES;
A computed column cannot have subquery of anykind and must use only data comming from the row's values. There is a possibility to put the query into an UDF and use the UDF for the computed column, but it is not well recommended...
A much proper way is to use a trigger
Related
I have a query (similar to the one below) that supposed to create a temporary table from a join:
SELECT p.pid,*
INTO #temp_insert_inventory
FROM
(SELECT *
FROM main_inventory.dbo.Inv i
LEFT JOIN web_inventory.dbo.Variant v ON i.id = v.ext4
WHERE v.ext4 IS NULL
AND i.qty > 0) PL
INNER JOIN web_inventory.dbo.Product p ON PL.invcode = p.ext
The problem is that SQL Server is refusing to create the temporary table because there are multiple CreatedOn columns being returned from the select.
I really don't need any one of these columns so how can I just omit it in the select?
Thanks!
List out all the columns instead of SELECT *.
If you need both versions of the conflicting CreatedOn columns, you can use an alias for one of them. If you don't want the additional column at all then don't include it in the SELECT list.
Bad Habits to Kick: Using SELECT * / omitting the column list
I keep coming across this DELETE FROM FROM syntax in SQL Server, and having to remind myself what it does.
DELETE FROM tbl
FROM #tbl
INNER JOIN tbl ON fk = pk AND DATEDIFF(day, #tbl.date, tbl.Date) = 0
EDIT: To make most of the comments and suggested answers make sense, the original question had this query:
DELETE FROM tbl
FROM tbl2
As far as I understand, you would use a structure like this where you are restricting which rows to delete from the first table based on the results of the from query. But to do that you need to have a correlation between the two.
In your example there is no correlation, which will effectively be a type of cross join which means "for every row in tbl2, delete every row in tbl1". In other words it will delete every row in the first table.
Here is an example:
declare #t1 table(A int, B int)
insert #t1 values (15, 9)
,(30, 10)
,(60, 11)
,(70, 12)
,(80, 13)
,(90, 15)
declare #t2 table(A int, B int)
insert #t2 values (15, 9)
,(30, 10)
,(60, 11)
delete from #t1 from #t2
The result is an empty #t1.
On the other hand this would delete just the matching rows:
delete from #t1 from #t2 t2 join #t1 t1 on t1.A=t2.A
I haven't seen this anywhere before. The documentation of DELETE tells us:
FROM table_source Specifies an additional FROM clause. This
Transact-SQL extension to DELETE allows specifying data from
and deleting the corresponding rows from the table in
the first FROM clause.
This extension, specifying a join, can be used instead of a subquery
in the WHERE clause to identify rows to be removed.
Later in the same document we find
D. Using joins and subqueries to data in one table to delete rows in
another table The following examples show two ways to delete rows in
one table based on data in another table. In both examples, rows from
the SalesPersonQuotaHistory table in the AdventureWorks2012 database
are deleted based on the year-to-date sales stored in the SalesPerson
table. The first DELETE statement shows the ISO-compatible subquery
solution, and the second DELETE statement shows the Transact-SQL FROM
extension to join the two tables.
With these examples to demonstrate the difference
-- SQL-2003 Standard subquery
DELETE FROM Sales.SalesPersonQuotaHistory
WHERE BusinessEntityID IN
(SELECT BusinessEntityID
FROM Sales.SalesPerson
WHERE SalesYTD > 2500000.00);
-- Transact-SQL extension
DELETE FROM Sales.SalesPersonQuotaHistory
FROM Sales.SalesPersonQuotaHistory AS spqh
INNER JOIN Sales.SalesPerson AS sp
ON spqh.BusinessEntityID = sp.BusinessEntityID
WHERE sp.SalesYTD > 2500000.00;
The second FROM mentions the same table in this case. This is a weird way to get something similar to an updatable cte or a derived table
In the third sample in section D the documentation states clearly
-- No need to mention target table more than once.
DELETE spqh
FROM
Sales.SalesPersonQuotaHistory AS spqh
INNER JOIN Sales.SalesPerson AS sp
ON spqh.BusinessEntityID = sp.BusinessEntityID
WHERE sp.SalesYTD > 2500000.00;
So I get the impression, the sole reason for this was to use the real table's name as the DELETE's target instead of an alias.
I have an insert Query and inserting data using SELECT query and certain joins between tables.
While running that query, it is giving error "String or binary data would be truncated".
There are thousands of rows and multiple columns I am trying to insert in that table.
So it is not possible to visualize all data and see what data is throwing this error.
Is there any specific way to identify which column is throwing this error? or any specific record not getting inserted properly and resulted into this error?
I found one article on this:
RareSQL
But this is when we insert data using some values and that insert is one by one.
I am inserting multiple rows at the same time using SELECT statements.
E.g.,
INSERT INTO TABLE1 VALUES (COLUMN1, COLUMN2,..) SELECT COLUMN1, COLUMN2,.., FROM TABLE2 JOIN TABLE3
Also, in my case, I am having multiple inserts and update statements and even not sure which statement is throwing this error.
You can do a selection like this:
select TABLE2.ID, TABLE3.ID TABLE1.COLUMN1, TABLE1.COLUMN2, ...
FROM TABLE2
JOIN TABLE3
ON TABLE2.JOINCOLUMN1 = TABLE3.JOINCOLUMN2
LEFT JOIN TABLE1
ON TABLE1.COLUMN1 = TABLE2.COLUMN1 and TABLE1.COLUMN2 = TABLE2.COLUMN2, ...
WHERE TABLE1.ID = NULL
The first join reproduces the selection you have been using for the insert and the second join is a left join, which will yield null values for TABLE1 if a row having the exact column values you wanted to insert does not exist. You can apply this logic to your other queries, which were not given in the question.
You might just have to do it the hard way. To make it a little simpler, you can do this
Temporarily remove the insert command from the query, so you are getting a result set out of it. You might need to give some of the columns aliases if they don't come with one. Then wrap that select query as a subquery, and test likely columns (nvarchars, etc) like this
Select top 5 len(Col1), *
from (Select col1, col2, ... your query (without insert) here) A
Order by 1 desc
This will sort the rows with the largest values in the specified column first and just return the rows with the top 5 values - enough to see if you've got a big problem or just one or two rows with an issue. You can quickly change which column you're checking simply by changing the column name in the len(Col1) part of the first line.
If the subquery takes a long time to run, create a temp table with the same columns but with the string sizes large (like varchar(max) or something) so there are no errors, and then you can do the insert just once to that table, and run your tests on that table instead of running the subquery a lot
From this answer,
you can use temp table and compare with target table.
for example this
Insert into dbo.MyTable (columns)
Select columns
from MyDataSource ;
Become this
Select columns
into #T
from MyDataSource;
select *
from tempdb.sys.columns as TempCols
full outer join MyDb.sys.columns as RealCols
on TempCols.name = RealCols.name
and TempCols.object_id = Object_ID(N'tempdb..#T')
and RealCols.object_id = Object_ID(N'MyDb.dbo.MyTable)
where TempCols.name is null -- no match for real target name
or RealCols.name is null -- no match for temp target name
or RealCols.system_type_id != TempCols.system_type_id
or RealCols.max_length < TempCols.max_length ;
I cannot get this type of select query (pseudo-code) to work. The UDF returns a table with 8 columns in a single row for a given 'UID_VEHICLE'. It works perfectly when the 'UID_VEHICLE' is provided as a constant like 3308. But I need one row of these function-results for each vehicle for a given customer -- up to 100 rows to be returned.
SELECT
*
FROM
[dbo].[fnGetNextDOT_InspectionData](UID_VEHICLE)
WHERE
UID_VEHICLE IN (SELECT UID_VEHICLE
FROM tVEHICLES
WHERE UID_CUSTOMER = 88);
Your comments and solutions are welcome...thanks...John
When passing row values from a query into a TVF, you need to use CROSS APPLY or OUTER APPLY (starting with SQL Server 2005):
SELECT * -- or dot.*, or whatever is desired
FROM tVEHICLES veh
CROSS APPLY [dbo].[fnGetNextDOT_InspectionData](veh.UID_VEHICLE) dot
WHERE veh.UID_CUSTOMER = 88;
I'm trying to update a column in one table (#TEMPTABLE) using data in another table (#PEOPLE) by using the REPLACE() function.
#TEMPTABLE has a column called "NameString' that is a long string with a user's name and ID.
#PEOPLE has a column for ID, and IDnumber.
UPDATE #TEMPTABLE
SET NAMEString = REPLACE(NAMEString, a.[ID], a.[IDNumber]) FROM #PEOPLE a
I'm trying to replace all the ID's in the NameString Column with the IDnumbers coming from #People table.
You need to give it a join criterion. For example:
update #TEMPTABLE
set NAMEString = replace(a.NAMEString, a.ID, b.IDNumber)
from #TEMPTABLE a
left join #PEOPLE b
on a.id = b.ID
Also, make sure to reference the right table when you use the IDNumber column - your original query doesn't actually use the table containing IDNumber at all, as far as I can tell from your description of your tables.
Note that my example assumes there's an ID field in #TempTable, or something else to join on - otherwise, you may need to extract it from the NameString column first.