Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert select statement using subquery

Author  Topic 

Shamee_1321
Starting Member

15 Posts

Posted - 2008-02-18 : 00:57:41
I have this statement for an insert into table article:

INSERT INTO Article
(
ArticleId,
CategoryThemeId,
Title,
DisplayOnHomePage,
StartDate,
EndDate,
ShortDescription,
Visual,
Thumbnail,
NumberOfVotes,
Marks,
StatusId,
[Type],
EtatValider,
UserName,
CreateDate,
AllowComments,
PreviewHtml,
IsInUserPostedTips,
ArticleIdXXX
)
SELECT NEWID(),--articleid
CASE WHEN seccion = 'belleza' THEN '5A60C79F-A5A9-4FA2-91A6-536569DD52D1'
WHEN seccion = 'moda' THEN '15BDADD1-E07C-44F8-889C-83C0E97FD9E8'
WHEN seccion = 'cocina' THEN '38A6642F-DA62-4C67-8697-B2833F9BF66F'
END, -- categorythemeid
titulo,--title
'0', --displayonhomepage
fecha_mod,-- startdate
NULL, -- enddate
resumen,-- shortdescription
NULL,--visual
NULL,--thumbnail
'0',--numberofvotes
'0',
CASE WHEN publicado = '1' THEN 'o'
WHEN publicado = '0' THEN 'f'
END, -- statusid
'2', -- is this always 2 becoz hints
'0', --etatvalider
(SELECT aspnet_membership.Email
FROM aspnet_membership INNER JOIN
ITGC_ES_TEMP.dbo.Usuarios on Usuarios.Email = aspnet_membership.Email collate SQL_Latin1_General_CP1_CI_AS
INNER JOIN ITGC_ES_TEMP.dbo.TRUCOS ON trucos.autor = Usuarios.id_usuario),--username
fecha_mod, --createdate
'1',--allowcomments
NULL,--previewhtml
'1',--isuserpostedtips
id_truco
FROM [itgc_es_temp].[dbo].trucos
WHERE (seccion = 'belleza' or seccion = 'moda' or seccion = 'cocina')

For username, i have used a subquery... I am having an error while executing the statement.. it is saying,
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I understand that the subquery is returning more than 1 values... but i can't find any soln for it...
please help!!!!

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2008-02-18 : 01:44:02
only i can suggest here is that, before inserting values, 1st try to execute the select statement whose o/p u r going to insert. u should write the query properly so that it can give u the correct o/p.

thanks,

Mahesh
Go to Top of Page

Shamee_1321
Starting Member

15 Posts

Posted - 2008-02-18 : 01:50:57
Without the subquery below which is used for inserting values for username, the select statement is working fine. The problem is with the subquery statement below... Is there another way of writing this subquery.

SELECT aspnet_membership.Email
FROM aspnet_membership INNER JOIN
ITGC_ES_TEMP.dbo.Usuarios on Usuarios.Email = aspnet_membership.Email collate SQL_Latin1_General_CP1_CI_AS
INNER JOIN ITGC_ES_TEMP.dbo.TRUCOS ON trucos.autor = Usuarios.id_usuario)


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-18 : 03:34:13
quote:
Originally posted by Shamee_1321

Without the subquery below which is used for inserting values for username, the select statement is working fine. The problem is with the subquery statement below... Is there another way of writing this subquery.

SELECT aspnet_membership.Email
FROM aspnet_membership INNER JOIN
ITGC_ES_TEMP.dbo.Usuarios on Usuarios.Email = aspnet_membership.Email collate SQL_Latin1_General_CP1_CI_AS
INNER JOIN ITGC_ES_TEMP.dbo.TRUCOS ON trucos.autor = Usuarios.id_usuario)





That really depends on what your requirements are. This query certainly involves one to many relationship which is reason for duplicates. While modifying this to return single value, you need to decide on what value you're interested in (latest value,first value, random value etc). Once this is fixed,we can rewrite the query.
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2008-02-18 : 05:35:30
Maybe:
SELECT NEWID() --articleid
,CASE T.seccion
WHEN 'belleza' THEN '5A60C79F-A5A9-4FA2-91A6-536569DD52D1'
WHEN 'moda' THEN '15BDADD1-E07C-44F8-889C-83C0E97FD9E8'
ELSE '38A6642F-DA62-4C67-8697-B2833F9BF66F'
END -- categorythemeid
,T.titulo--title
,'0' --displayonhomepage
,T.fecha_mod-- startdate
,NULL -- enddate
,T.resumen-- shortdescription
,NULL --visual
,NULL --thumbnail
,'0'--numberofvotes
,'0'
,CASE T.publicado
WHEN '1' THEN 'o'
ELSE 'f'
END -- statusid
,'2' -- is this always 2 becoz hints
,'0' --etatvalider
,D.EMail --username
,T.fecha_mod --createdate
,'1'--allowcomments
,NULL--previewhtml
,'1'--isuserpostedtips
,T.id_truco
FROM itgc_es_temp.dbo.trucos T
LEFT JOIN
(
SELECT
ROW_NUMBER() OVER (PARTITION BY U.id_usuario ORDER BY U.EMail) AS RowId
,U.id_usuario AS autor
,U.EMail
FROM ITGC_ES_TEMP.dbo.Usuarios U
WHERE EXISTS
(
SELECT *
FROM aspnet_membership.Email M
WHERE M.EMail COLLATE SQL_Latin1_General_CP1_CI_AS = U.EMail
)
) D
ON T.autor = D.autor
AND D.RowId = 1
WHERE T.seccion IN ('belleza', 'moda', 'cocina')
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-18 : 05:44:07
A simple TOP 1 would do find. But We still don't know how to relate the subquery with the outer query..
INSERT		Article
(
ArticleId,
CategoryThemeId,
Title,
DisplayOnHomePage,
StartDate,
EndDate,
ShortDescription,
Visual,
Thumbnail,
NumberOfVotes,
Marks,
StatusId,
[Type],
EtatValider,
UserName,
CreateDate,
AllowComments,
PreviewHtml,
IsInUserPostedTips,
ArticleIdXXX
)
SELECT NEWID(),--articleid
CASE seccion
WHEN 'belleza' THEN '5A60C79F-A5A9-4FA2-91A6-536569DD52D1'
WHEN 'moda' THEN '15BDADD1-E07C-44F8-889C-83C0E97FD9E8'
WHEN 'cocina' THEN '38A6642F-DA62-4C67-8697-B2833F9BF66F'
END, -- categorythemeid
titulo,--title
'0', --displayonhomepage
fecha_mod,-- startdate
NULL, -- enddate
resumen,-- shortdescription
NULL,--visual
NULL,--thumbnail
'0',--numberofvotes
'0',
CASE publicado
WHEN '1' THEN 'o'
WHEN '0' THEN 'f'
END, -- statusid
'2', -- is this always 2 becoz hints
'0', --etatvalider
(SELECT TOP 1 aspnet_membership.Email
FROM aspnet_membership
INNER JOIN ITGC_ES_TEMP.dbo.Usuarios on Usuarios.Email = aspnet_membership.Email collate SQL_Latin1_General_CP1_CI_AS
INNER JOIN ITGC_ES_TEMP.dbo.TRUCOS ON trucos.autor = Usuarios.id_usuario),
--username
fecha_mod, --createdate
'1',--allowcomments
NULL,--previewhtml
'1',--isuserpostedtips
id_truco
FROM [itgc_es_temp].[dbo].trucos
WHERE seccion in ('belleza', 'moda', 'cocina')



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -