sql insert into with sub-query - sql

I want to run this query
INSERT INTO [tblPollLogs]
([lastModified]
,[ip]
,[a1]
,[a2]
)
VALUES
(getdate()
,'aaa'
,(select top 1 header from [tblPollAnswer] where [pollAnswerId] = #param1)
,(select top 1 header from [tblPollAnswer] where [pollAnswerId] = #param2)
)
But I get the error
Subqueries are not allowed in this context. Only scalar expressions are allowed.
can i run this query in one query or i need get the values before running this query
thanks

Try to change it to insert...select as below
INSERT INTO [tblPollLogs]
([lastModified]
,[ip]
,[a1]
,[a2]
)
select getdate(),
'aaa',
(select top 1 header from [tblPollAnswer] where [pollAnswerId] = #param1),
(select top 1 header from [tblPollAnswer] where [pollAnswerId] = #param2)

Related

When select from insert into returns no values do something different

I want to insert rows into a table. The table is empty when I start. My query is as follows:
Select TOP 1 *
INTO #Result
FROM #SmallTable
WHERE CategoryID=11
ORDER BY ExpValue DESC;
It works flawless. But I want now to account for the case where the this returns no value. But I'm not sure how to approach this.
I could either make a case and select and ask if SELECT TOP 1 returns any values. Or I could check after I insert if there is a value present. But which approach would be better? Or is there an even better one?
You could use a union trick here to insert a dummy value should the first query not return any records:
INSERT INTO #Result (col)
SELECT TOP 1 col
FROM
(
SELECT TOP 1 col, 1 AS pos FROM #SmallTable WHERE CategoryID = 11 ORDER BY ExpValue DESC
UNION ALL
SELECT 'NA', 2
) t
ORDER BY pos;
Look at ##ROWCOUNT
This returns the number of rows affected by the last procedure.
IF ##ROWCOUNT = 0
PRINT 'Warning: No rows were inserted';
You can use apply :
select top (1) coalesce(st.CategoryID, 0) as CategoryID, . .
into #destination
from ( values (11)
) t(CategoryID) left join
#SmallTable st
on st.CategoryID = t.CategoryID
order by st.ExpValue desc;

SQL - Run Select Statement Based On Where Query

Hi i want to create a query which does the following. When the paramter 25 is selected it only runs part A of the query, if any other number is selected run both Table A and B select queries.
Example Below:
DECLARE #Type varchar (200)
select * from
(SELECT sort_code FROM dbo.Test 1
WHERE FUNDING_YEAR = 26)
union
(SELECT sort_code FROM dbo.Test 2
WHERE FUNDING_YEAR = 26)
Where case when #Type = 25 then select * from table2 else table 1
You just need to reference the variable in the WHERE clause
SELECT *
FROM TableA
WHERE #Type = 25
UNION
SELECT *
FROM TableB
The query above will always select everything in TableB and will only select everything in TableA when the variable is equal to 25.
Since you are using SSRS, what I would do is write the query to return all of the rows and then apply a filter in the SSRS report when the Paramater is 25. I wouldn't pass a paramater value to the SQL side unless it greatly reduces the run time of the query.
(I would have put this in a comment.)

SQL Subquery returned more than 1 value

My query causes the following error:
Msg 512, Level 16, State 1, Procedure Item_insupd, Line 17
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Query:
INSERT INTO [Total_Database].[dbo].[Item]
(
ItemID,
ItemNo,
ItemDescription,
Notes,
StandardCost,
SalesGLAccountID,
ItemTypeID,
Backorderable
)
(
SELECT [nr],
[nr],
[Latijn]+' '+[Subgroep]+' '+CAST([nr] as VARCHAR(255)),
[NL]+' '+[Vorm]+' '+[Kenmerk]+' '+[Hoogte],[Inkoopprijs],
(4),
(case when [Productgroep]='PB' then 1 else 5 end),
(1)
FROM [ACCESDATA].[dbo].[Planten]
);
I suspect this to happen because my subquery does not contain a WHERE, unfortunately I do not know how to construct a correct WHERE clause.
I suspect the problem is in this string (line 26 in your code):
IF NOT (EXISTS (SELECT G.GLAccountID FROM GLAccnt G INNER JOIN Inserted I ON G.GLAccountID = I.SalesGLAccountID))
OR ((SELECT I.COGSGLAccountID FROM Inserted I) IS NOT NULL) AND NOT (EXISTS (SELECT G.GLAccountID FROM GLAccnt G INNER JOIN Inserted I ON G.GLAccountID = I.COGSGLAccountID))
It looks like (SELECT I.COGSGLAccountID FROM Inserted I) return more than one row, so you're getting an error.
You're treating inserted as a one row table (for example, you're getting parameters from it like SELECT #ItemNo = I.ItemNo, #ItemDescription = I.ItemDescription FROM Inserted I, but inserted table can have more than one row. So in your case I think you have 3 options - check that there's only 1 row in inserted, rewrite trigger as set-based, or use cursor.
Here's sql fiddle with somewhat similar example.
If you really only want to insert one row, then You just add Where at the end, followed by some predicate (logical statement) that will be true for only one row in the table the query is reading from.
INSERT INTO [Total_Database].[dbo].[Item](ItemID,
ItemNo,ItemDescription,Notes,StandardCost,SalesGLAccountID,
ItemTypeID,Backorderable)
SELECT [nr],[nr],[Latijn]+' '+[Subgroep]+' '+CAST([nr] as VARCHAR(255)),
[NL]+' '+[Vorm]+' '+[Kenmerk]+' '+[Hoogte],[Inkoopprijs],(4),
(case when [Productgroep]='PB' then 1 else 5 end),(1)
FROM [ACCESDATA].[dbo].[Planten]
Where [SomeColumnName] = [some Value];
... but when performing an Insert using a select to generate the rows to be inserted, you just type the Select statement instead of the Values() clause, without surrounding parentheses. I think that because you had the surrounding parentheses, the query processor assumed you wanted that to be a single value. Do you want to only insert one row of data? or a whole set of data rows ?
INSERT INTO [Total_Database].[dbo].[Item]
(
ItemID,
ItemNo,
ItemDescription,
Notes,
StandardCost,
SalesGLAccountID,
ItemTypeID,
Backorderable
) IN
(
SELECT [nr],
[nr],
[Latijn]+' '+[Subgroep]+' '+CAST([nr] as VARCHAR(255)),
[NL]+' '+[Vorm]+' '+[Kenmerk]+' '+[Hoogte],[Inkoopprijs],
(4),
(case when [Productgroep]='PB' then 1 else 5 end),
(1)
FROM [ACCESDATA].[dbo].[Planten]
);

How to insert bulky value to sql table?

I want to add bulky data from another data below. But I can not do that: error is returned. But areas are the same as another.
declare #hrmtable1 table(musterino int, ekno smallint)
insert into #hrmtable1 (musterino , ekno)
select distinct musterino, ekno
from hareketmuhasebe (nolock)
where islemtarihi >= '20120101'
and isnull(musterino, 0) <> 0
and isnull(musterino, 0) > 9000000
and isnull(ekno,0) <> 0
insert into table1(A,B,C,D,E,. . . . .N)
SELECT DISTINCT
case when ((select count(*) from table1 where musterino=e.musterino) > 0)
then (select top 1 *
from dbo.table1
where musterino = e.musterino
order by ekno desc)
else
(select 100, e.musterino, e.ekno, 0, K, L, M)
from #hrmtable1 e )
end
ERROR:
Msg 120, Level 15, State 1, Line 10
The select list for the INSERT statement contains fewer items than the insert list.
The number of SELECT values must match the number of INSERT columns.
As mentioned in the error the number of columns in insert does not mach the number of value that you are providing
insert into #hrmtable1 (musterino , ekno)
select distinct musterino,ekno
from hareketmuhasebe (nolock)
where islemtarihi >= '20120101'
and isnull(musterino,0) <> 0 and isnull(musterino,0) < 9000000 and isnull(ekno,0) <> 0
Try this:
insert into #hrmtable1 (musterino , ekno)
select distinct musterino,ekno
from hareketmuhasebe (nolock)
where islemtarihi >= '20120101'
and isnull(musterino,0) <> 0 and isnull(musterino,0) < 9000000 and isnull(ekno,0) <> 0
You are having 2 columns in insert & 5 columns in select clause, that is the cause of error.
EDIT: Since you edited the question, still the answer remains the same. Your number of columns in Select statement doesn't match to your insert statement columns in the 2nd part of your code.
In your first statement you are specifying 2 columns and in select you have 5 columns, they should match and that is what the error is telling you exactly.
insert into #hrmtable1 (musterino , ekno)
^^^^^^^ ^^^^
select distinct musterino,ekno,defterid,dovizcinsi, subekodu
^^^^^^^ ^^^ ^^^^ ^^^^^ ^^^^^
From your insert statement it looks like you don't need the last three columns in your Select statement.
The select list for the INSERT statement contains fewer items than the
insert list. The number of SELECT values must match the number of
INSERT columns.
The error itself tells you the story that what you are doing wrong. you have fewer columns in insert statement than to select statement.
From MSDN
The select list of the subquery must match the column list of the
INSERT statement. If no column list is specified, the select list must
match the columns in the table or view being inserted into.
Read more about Inert into

Select the rows that just inserted

How can I retrieve the row just inserted?
INSERT INTO LETTRE_VOIT
select rsVoit.NOID, NO_ORDRE, rsOrdre.CODE_DEST, rsOrdre.MODAL_MODE, rsOrdre.MODAL_PORT,
CASE rsOrdre.MODAL_PORT
WHEN 'false' THEN 'D'
ELSE 'P'
END,
rsOrdre.LIVRS_EXPRS,
CASE rsOrdre.LIVRS_EXPRS
WHEN 'false' THEN 'L'
ELSE 'E'
END,
rsOrdre.ENLEV_UNITE, LIBELLE, NBR_COLIS,POID,ENLEV_CREMB,ENLEV_DECL
from ORDRE rsOrdre
inner join
(
select CODE_DEST,MODAL_MODE, MODAL_PORT, LIVRS_EXPRS,ENLEV_UNITE, ROW_NUMBER() over (order by CODE_DEST) as NOID
from ORDRE
group by CODE_DEST,MODAL_MODE,MODAL_PORT,LIVRS_EXPRS,ENLEV_UNITE
) rsVoit on rsVoit.CODE_DEST = rsOrdre.CODE_DEST and rsVoit.MODAL_MODE = rsOrdre.MODAL_MODE
and rsVoit.MODAL_PORT = rsOrdre.MODAL_PORT and rsVoit.LIVRS_EXPRS = rsOrdre.LIVRS_EXPRS
and rsVoit.ENLEV_UNITE = rsOrdre.ENLEV_UNITE
LEFT JOIN T_UNITE ON rsOrdre.ENLEV_UNITE = T_UNITE.NOID
WHERE (STATUT_ORDRE = 3) AND IS_PRINT = 'false' AND (TRANSPORTEUR IN (SELECT ParsedString From dbo.ParseStringList(#Trans)))
order by rsVoit.NOID, NO_ORDRE
SELECT * FROM LETTRE_VOIT WHERE ???
For example:
I inserted 2, it return 2
and then I inserted 3, I want return 3 instead of 5 rows.
Thanks in advance.
Stev
PS: May be I need to use a stored procedure ?
I'm not 100% sure what exactly you want back.... but SQL Server has an OUTPUT clause that can output stuff from INSERT and UPDATE and DELETE statements:
INSERT INTO dbo.YourTable(col1, col2, ..., colN)
OUTPUT Inserted.Col1, Inserted.IDCol, Inserted.Col17
VALUES(val1, val2, ...., valN)
Here, you're inserting values and the inserted values for the IDCol (e.g. an INT IDENTITY column), Col1 and Col17.
If just getting back the results into your grid in Mgmt Studio is good enough - then use the OUTPUT clause! Read more about the OUTPUT clause on Books Online
If you are using SQL Server
And you know how many row inserted then go through
SELECT top 2 * FROM LETTRE_VOIT order by primaryKeyId desc
put the number of row inserted at place of 2.
It may be help you, If you know the number of inserted rows, and then you can provide the numbers with top keyword
How about saving the records in a variable before inserting them or adding a date field and retrieve them by date?