Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


for me problem this select insert


for me problem this select insert

Author
Message
gurbanov.1984
gurbanov.1984
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 113
create table table1
(phone int,kod_name int ,kod_fname int,kod_country int,kod_auto int) --

create table table2 (name_ nvarchar(30),kod int)
insert into table2 values('james',1)
insert into table2 values('stivens',2)
insert into table2 values('carlos',3)



create table table3 (f_name nvarchar(30),kod int)
insert into table2 values('john',1)
insert into table2 values('tayson',2)
insert into table2 values('swarzneger',3)




create table table4 (country nvarchar(30),kod int)
insert into table2 values('argentina',1)
insert into table2 values('brazilia',2)
insert into table2 values('korea',3)


create table table5 (m_auto nvarchar(30),kod int)
insert into table2 values('mersedec',1)
insert into table2 values('jaguar',2)
insert into table2 values('landrover',3)


how i make created procedure

for insert to table1

when name=james then in kod_name_d insert 1 (kod table2)
when fname=jhon then in kod_fname insert 1 (kod table3 )
when country=korea then in kod_country insert 3 (kod table4 )
when auto=jaguar then in kod_auto insert 2 (kod table5 )

forexamle

i inserted table 1

(11111,'james','jhon','korea','jaguar')

after inserted table1

table1
-----------
phone kod_name kod_fname kod_country kod_auto
----- -------- --------- ----------- --------
11111 1 1 3 2
gurbanov.1984
gurbanov.1984
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 113
i created procedure

but this procedure inserted only phone



create procedure inserttable1
@phone int,
@n nvarchar(30),@f nvarchar(30),@c nvarchar (30),@t nvarchar(30)
as
insert table1 select @phone,(select kod from table2 where name_='@n'),(select kod from table3 where f_name='@f'),
(select kod from table4 where country='c'),(select kod from table5 where m_auto='@t')

exec inserttable1 1111,'stivens','tayson','argentina','mersedec'

select*from table1
laurie-789651
laurie-789651
SSC-Addicted
SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)

Group: General Forum Members
Points: 424 Visits: 1271
You need to join the lookup tables to the inputs to convert to the key values:



--== SAMPLE DATA ==--
if object_id('table1') is not null
drop table table1;
if object_id('table2') is not null
drop table table2;
if object_id('table3') is not null
drop table table3;
if object_id('table4') is not null
drop table table4;
if object_id('table5') is not null
drop table table5;

create table table1
(phone int,kod_name int ,kod_fname int,kod_country int,kod_auto int) --

create table table2 (name_ nvarchar(30),kod int)
insert into table2 values('james',1)
insert into table2 values('stivens',2)
insert into table2 values('carlos',3)

create table table3 (f_name nvarchar(30),kod int)
insert into table3 values('john',1)
insert into table3 values('tayson',2)
insert into table3 values('swarzneger',3)

create table table4 (country nvarchar(30),kod int)
insert into table4 values('argentina',1)
insert into table4 values('brazilia',2)
insert into table4 values('korea',3)

create table table5 (m_auto nvarchar(30),kod int)
insert into table5 values('mersedec',1)
insert into table5 values('jaguar',2)
insert into table5 values('landrover',3)

--== EXPECTED RESULTS ==--
--how i make created procedure

--for insert to table1

--when name=james then in kod_name_d insert 1 (kod table2)
--when fname=jhon then in kod_fname insert 1 (kod table3 )
--when country=korea then in kod_country insert 3 (kod table4 )
--when auto=jaguar then in kod_auto insert 2 (kod table5 )

--forexamle

--i inserted table 1

--(11111,'james','jhon','korea','jaguar')

--after inserted table1

--table1
-------------
--phone kod_name kod_fname kod_country kod_auto
------- -------- --------- ----------- --------
--11111 1 1 3 2

--== SOURCE DATA ==--
if object_id('source_data') is not null
drop table source_data;

create table source_data
(
s_phone nvarchar(30),s_name nvarchar(30) ,s_fname nvarchar(30),s_country nvarchar(30),s_auto nvarchar(30)
)
insert into source_data values
(
11111,'james','john','korea','jaguar'
)

--== SUGGESTED SOLUTION ==--
insert into table1
(phone,kod_name,kod_fname,kod_country,kod_auto)
select
source_data.s_phone,
table2.kod,
table3.kod,
table4.kod,
table5.kod
from source_data
inner join table2 on s_name = name_
inner join table3 on s_fname = f_name
inner join table4 on s_country = country
inner join table5 on s_auto = m_auto

select * from table1



laurie-789651
laurie-789651
SSC-Addicted
SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)

Group: General Forum Members
Points: 424 Visits: 1271
gurbanov.1984 (7/6/2013)
i created procedure

but this procedure inserted only phone



create procedure inserttable1
@phone int,
@n nvarchar(30),@f nvarchar(30),@c nvarchar (30),@t nvarchar(30)
as
insert table1 select @phone,(select kod from table2 where name_='@n'),(select kod from table3 where f_name='@f'),
(select kod from table4 where country='c'),(select kod from table5 where m_auto='@t')

exec inserttable1 1111,'stivens','tayson','argentina','mersedec'

select*from table1


You don't need the quotes round the parameter names:


create procedure inserttable1
@phone int,
@n nvarchar(30),@f nvarchar(30),@c nvarchar (30),@t nvarchar(30)
as

insert table1
(phone,kod_name,kod_fname,kod_country,kod_auto)
select @phone,(select kod from table2 where name_=@n),(select kod from table3 where f_name=@f),
(select kod from table4 where country=@c),(select kod from table5 where m_auto=@t)



gurbanov.1984
gurbanov.1984
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 113
THANK YOU VERY MUCH VETERAN

but i created this


create procedure inserttable1
@phone int,
@n nvarchar(30),@f nvarchar(30),@c nvarchar (30),@t nvarchar(30)
as

insert table1
(phone,kod_name,kod_fname,kod_country,kod_auto)
select @phone,(select kod from table2 where name_=@n),(select kod from table3 where f_name=@f),
(select kod from table4 where country=@c),(select kod from table5 where m_auto=@t)

insert test
(phone,kod_name,kod_fname,kod_country,kod_auto)
select
phone,kod_name,kod_fname,kod_country,kod_auto from table1 where phone=@phone and phone is not null and phone<>0


exec inserttable1 '','stivens','tayson','argentina','mersedec'





thank you very much
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search