472,114 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,114 software developers and data experts.

How to convert from sql to postgresql (stored procedure)

I have to convert this script, but i didn't. This is stored procedure of sql. I need to convert to postgresql...


if it is possible please help me!
Thanks
Fatma

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE PROCEDURE usp_generatetable(@prm_tableID int, @prm_langID int)
  3. as 
  4. begin
  5.     declare @table_id int, @field_id int, @match_code varchar(50), @value varchar(250), @first_field int, 
  6.         @query_text varchar(8000), @table_identity varchar(30), @active_match_code varchar(250),
  7.         @dil_id varchar(20), @counter int, @rec_counter int, @data_type varchar(50), @field_name varchar(50)
  8.  
  9.     set nocount on
  10.  
  11.     declare cur_field_list cursor for
  12.         select fieldID from tbl_fields where tableID = @prm_tableID and field_isactive = 1 order by fieldID
  13.  
  14.  
  15.     select @table_identity = 'temp_' + replace(convert(varchar(50), getdate(), 112) + convert(varchar(50), getdate(), 114), ':', '')
  16.  
  17.     set @query_text = 'create table ' + @table_identity + ' (rec_match_code varchar(20), '
  18.  
  19.     set @first_field = 1
  20.  
  21.     open cur_field_list
  22.  
  23.     fetch next from cur_field_list into @field_id
  24.  
  25.     while @@fetch_status = 0
  26.         begin
  27.             select @field_name = field_name, @data_type = field_datatype from tbl_fields where fieldID = @field_id
  28.  
  29.             if @first_field = 1
  30.                 set @query_text = @query_text + @field_name + ' ' + @data_type
  31.             else
  32.                 set @query_text = @query_text + ', ' + @field_name + ' ' + @data_type
  33.  
  34.             fetch next from cur_field_list into @field_id
  35.  
  36.             set @first_field = 0
  37.         end
  38.  
  39.     set @query_text = @query_text + ' ) '
  40.  
  41.     close cur_field_list
  42.  
  43.     deallocate cur_field_list    
  44.  
  45.     EXEC (@query_text)
  46.  
  47.     declare cur_source_data cursor for
  48.         select fieldID, rec_match_code, rec_value
  49.         from tbl_records
  50.         where tableID = @prm_tableID and langID = @prm_langID
  51.         order by rec_match_code, fieldID
  52.  
  53.     open cur_source_data
  54.     --print @@cursor_rows
  55.  
  56.     set @active_match_code = ''
  57.  
  58.     fetch next from cur_source_data
  59.         into @field_id, @match_code, @value
  60.  
  61.     while @@fetch_status = 0 
  62.         begin
  63.             if @active_match_code <> @match_code
  64.                 begin
  65.                     set @query_text = 'insert into ' + @table_identity + ' (rec_match_code) values ( ' + '''' + @match_code + '''' + ' ) '
  66.  
  67.                     EXEC (@query_text)
  68.  
  69.  
  70.                     set @active_match_code = @match_code                
  71.                 end
  72.  
  73.             select @field_name = field_name, @data_type = field_datatype from tbl_fields where fieldID = @field_id
  74.  
  75.             set @query_text = 'update ' + @table_identity + ' set ' + @field_name + ' = ' + 'convert(' + @data_type + ', ' + '''' + @value + '''' + ') ' + ' where rec_match_code = ' + '''' + @match_code + ''''
  76.  
  77.             EXEC (@query_text)
  78.  
  79.             fetch next from cur_source_data
  80.                 into @field_id, @match_code, @value
  81.         end
  82.     close cur_source_data
  83.  
  84.     deallocate cur_source_data
  85.  
  86.  
  87.     EXEC('select * from ' + @table_identity)
  88.  
  89.     set @query_text = 'if exists (select * from dbo.sysobjects where name like ' + '''' + '%' + @table_identity + '%' + '''' + ') drop table ' + @table_identity
  90.  
  91.     exec(@query_text) 
  92.  
  93.  
  94.  
  95. end
  96. GO
  97.  
Aug 31 '06 #1
1 8350
doss
2
Boss,

There are no conceptes absolutly for PROCEDURE in Postgres SQL

I dont think so.

But u can make them as FUNCTIONS in Postgres SQL

I have to convert this script, but i didn't. This is stored procedure of sql. I need to convert to postgresql...


if it is possible please help me!
Thanks
Fatma

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE PROCEDURE usp_generatetable(@prm_tableID int, @prm_langID int)
  3. as 
  4. begin
  5.     declare @table_id int, @field_id int, @match_code varchar(50), @value varchar(250), @first_field int, 
  6.         @query_text varchar(8000), @table_identity varchar(30), @active_match_code varchar(250),
  7.         @dil_id varchar(20), @counter int, @rec_counter int, @data_type varchar(50), @field_name varchar(50)
  8.  
  9.     set nocount on
  10.  
  11.     declare cur_field_list cursor for
  12.         select fieldID from tbl_fields where tableID = @prm_tableID and field_isactive = 1 order by fieldID
  13.  
  14.  
  15.     select @table_identity = 'temp_' + replace(convert(varchar(50), getdate(), 112) + convert(varchar(50), getdate(), 114), ':', '')
  16.  
  17.     set @query_text = 'create table ' + @table_identity + ' (rec_match_code varchar(20), '
  18.  
  19.     set @first_field = 1
  20.  
  21.     open cur_field_list
  22.  
  23.     fetch next from cur_field_list into @field_id
  24.  
  25.     while @@fetch_status = 0
  26.         begin
  27.             select @field_name = field_name, @data_type = field_datatype from tbl_fields where fieldID = @field_id
  28.  
  29.             if @first_field = 1
  30.                 set @query_text = @query_text + @field_name + ' ' + @data_type
  31.             else
  32.                 set @query_text = @query_text + ', ' + @field_name + ' ' + @data_type
  33.  
  34.             fetch next from cur_field_list into @field_id
  35.  
  36.             set @first_field = 0
  37.         end
  38.  
  39.     set @query_text = @query_text + ' ) '
  40.  
  41.     close cur_field_list
  42.  
  43.     deallocate cur_field_list    
  44.  
  45.     EXEC (@query_text)
  46.  
  47.     declare cur_source_data cursor for
  48.         select fieldID, rec_match_code, rec_value
  49.         from tbl_records
  50.         where tableID = @prm_tableID and langID = @prm_langID
  51.         order by rec_match_code, fieldID
  52.  
  53.     open cur_source_data
  54.     --print @@cursor_rows
  55.  
  56.     set @active_match_code = ''
  57.  
  58.     fetch next from cur_source_data
  59.         into @field_id, @match_code, @value
  60.  
  61.     while @@fetch_status = 0 
  62.         begin
  63.             if @active_match_code <> @match_code
  64.                 begin
  65.                     set @query_text = 'insert into ' + @table_identity + ' (rec_match_code) values ( ' + '''' + @match_code + '''' + ' ) '
  66.  
  67.                     EXEC (@query_text)
  68.  
  69.  
  70.                     set @active_match_code = @match_code                
  71.                 end
  72.  
  73.             select @field_name = field_name, @data_type = field_datatype from tbl_fields where fieldID = @field_id
  74.  
  75.             set @query_text = 'update ' + @table_identity + ' set ' + @field_name + ' = ' + 'convert(' + @data_type + ', ' + '''' + @value + '''' + ') ' + ' where rec_match_code = ' + '''' + @match_code + ''''
  76.  
  77.             EXEC (@query_text)
  78.  
  79.             fetch next from cur_source_data
  80.                 into @field_id, @match_code, @value
  81.         end
  82.     close cur_source_data
  83.  
  84.     deallocate cur_source_data
  85.  
  86.  
  87.     EXEC('select * from ' + @table_identity)
  88.  
  89.     set @query_text = 'if exists (select * from dbo.sysobjects where name like ' + '''' + '%' + @table_identity + '%' + '''' + ') drop table ' + @table_identity
  90.  
  91.     exec(@query_text) 
  92.  
  93.  
  94.  
  95. end
  96. GO
  97.  
Nov 24 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by deprins | last post: by
10 posts views Thread by Thomas R. Hummel | last post: by
4 posts views Thread by Henrik Juul | last post: by
6 posts views Thread by yin_n_yang74 | last post: by
4 posts views Thread by Alchemist | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.